Simple animation

Hi,I am a beginner learning xojo. I recently saw an excellent video called basic animation, using the timer control. It worked well and inspired me to try more. I am creating a dice game and created 5 canvas controls using 6 frames representing different faces of the dice finally stopping on the face I want. It works great for all 5 dice with realistic “rolling of the dice”. I disable the timers during the screen open event, then reenable them with a button called “roll”. All this works well. The question I have is how can I set it up so I can re-roll a particular dice, or even re-roll all dice. It always displays the initial roll and nothing seems to happen. I am sorry that this may be a trivial matter, and any documentation I have read gets complex very quickly (although I am sure it’s all there somewhere) Would really appreciate any help.
Thanks, Carl.

It really depends on how you have implemented things as there are many different ways to do this and its a little tricky to advise on which direction you should go next without know which route you went.

E.g. if you have a variable which is your target value (the dice roll) and a current frame counter and things stop when the two match then you just need to move the target value of one of the dice and the roll will happen until it gets back to the two matching each other.

This is of course if you just leave the timer running. If you stop the timer when all dice hit their target then you will need to have a little rethink of your code so you can stop other dice rolling when they hit their targets with the timer running all the time.

When you get to this stage, it’ll just be a matter of adding a MouseDown event to the canvas that will reroll its dice target value.

If you get stuck, post a link to your binary and I’m sure people will happily help you in the right direction from where you are.

1 Like

Thanks Julian I really appreciate your response. I should have made things clearer. I have 5 canvases and 5 timer events one for each canvas. The dice are just images to display multiple faces and finally the dice face for the number I want, and do not contribute to any calculation. I have a variable assigned to a number generated by RND function. For each canvas I perform a case construct - like case 0(frame) canvas1.background. face01, and so on,finally case 5 canvas1.background. (say)dice 3 (which was the value of the variable for the number I finally want to display. All other canvas coding is exactly the same. The timer action event for each timer contains the code. All this works well, but just stops after all dice have displayed and I can’t restart it or change any dice to display a different number.

I think I understand the way you’ve implemented it. If you move the variable that holds the roll value out of the timer code and make it a property of the window, you will be able to access that from other controls, like the reroll button. The reroll button can then change this value and start the timer.

If you’re still stuck and if noone has posted any demo code to help you I’ll whip something up later as I’m about to afk.

Thank you Julian - you have been fantastic help!

Sorry to bother you Julian. Still can’t get it to work. My roll value variable and all other variables have always been properties of the window and declared global. The only code in the Timer Action event is assigning a dice graphic as a backdrop. It starts with
Select case me.frame1
case 0
Frame1,background = dice4
… and so on, the end Select and increment the frame number.
No other code resides in this event. The code is the same for all other timers.

Show us your code or make an example.

Don’t use the BackDrop for drawing. Use the Paint event instead. And use one Canvas and not multiples of them.

Here’s a really simple implementation as an example.

https://blog.samphire.net/private/xojo/forum/DiceTestForCarl.xojo_binary_project

Check the comments and the Message Pane to see the debug output

image

The next step along would be to move this over to either a control set to allow multiple dice or into a container to make the dice “self contained” so you can drop as many as you want on the window.

Let me know how you get on.

Edit: Redownload to see a slightly cleaner version

i created also a dice roll via class
MRDiceTestForCarl.xojo_binary_project
https://drive.google.com/file/d/1C5VyPVQ9ynTBv4hmkK7-6wCEGc8wEwJ-/view?usp=sharing

you could subclass this canvas to have functionality there.

2 Likes

Markus - Appreciate your response, thank you very much for the link, I will certainly try your suggestion.

1 Like

Thanks again Julian. I am about to investigate the link you sent. You have been a terrific help. Thanks!

Thanks for your response Beatrix. I will try your suggestion - this is a real learning curve for me> I will let you know how I get on. Thanks again!

We have all been there, so no worries.