Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Help With Simple Program

Author
Message
FrankieV
14
Years of Service
User Offline
Joined: 10th Jun 2010
Location:
Posted: 11th Jun 2010 06:01
I'm fairly new to the Dark GDK and C++ in general. I'm trying to write this simple program that detects a mouseclick over a sprite, then plays a sprite animation and finally displays a random sprite. It a coin toss game in short. However I currently have 3 issues:
1) I cannot get the mouseclick to do anything, but my program moves forward with a key press, I believe this is due to the if statement in my game loop.
2)The Sprite animation will not play, this has to be something Im overseeing.
3)The random integer call in my cointoss function does not seem to really return a random number, it always returns 1!

The other thing I'd like to do is loop this so that you can continue to click on the coin and keep "flipping" it till you hit esc.

Thanks to anyone who takes the time to look this over for me, shouldn't take to long as I'm sure this is an elementary level program.

JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 11th Jun 2010 17:27
1) mouseX/mouseY are not being updated. Place these calls before the call to overSprite()



2) I'll have to get back with you on this one; I don't deal with animated sprites myself, so I can't answer it at this momemnt.

3) I would change the call to dbRND() and test for odd/even and use that:



Hope this helps you,

JTK
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 11th Jun 2010 17:41 Edited at: 11th Jun 2010 17:50
Question (3) random numbers:

Search the Dark GDK help for dbRND and you will find two useful pieces of information. First, that dbRND returns a number between zero and the range provided, so by using dbRND(2) you can receive not only 0 and 1 but also 2 as a result, which your pogram does not handle. Use dbRND(1) instead. (Edit: or the method suggested by JTK above.)

Second, the search throws up another function name as well, dbRandomize. To make the generator return really random numbers you have to "seed" (initialize) it also with a "random" number, for example the current time. Place this call somewhere into your setup function:



Question (2) sprite animation:

The problem with dbPlaySprite is that it doesn't work as you expect (and this is not in the help). It does not play the sprite animation from beginning to end. It plays only one frame when you call it once. When it is called the next time, it checks how much time has passed and either plays the same frame again or moves onto the next frame, depending on the speed setting. You need to call this function repeatedly, again and again, until the animation is finished. Note also that after every call you need to refresh the display (dbSync), otherwise you don't see any change on the screen.

How you accomplish screen refresh is a program design question. If you have a game state system in the program (like the one implemented in the Dark Invaders tutorial), it can be accomplished in the main loop. Without such system, you can also try this:



This will animate the sprite until frame 4 is reached, then quit and leave frame 4 permanently displayed while the execution of your program continues with the next line after the while loop. This may be suitable for your purpose but be aware that it's a problem if there is anything else on the screen (e.g. text) that would need repeated drawing commands before a screen refresh.


Question (1) mouse click:

(Edit) JTK is right, you should use dbMouseX and dbMouseY to get the current mouse coordinates.

By the way, just out of curiosity, not so long ago I gave advice to someone who had the exact same mouse-over-sprite function, does that come from some book?


Other suggestions:

In the setup part, you do not need to load the coin animation image. The dbCreateAnimatedSprite function will load the image from disk and it will assign the image number before creating the sprite.

I'm not sure that you really need three different sprites. If the head image and the tail image are included in the animation sequence, then it would be possible to use just one sprite. When you need a head or tail image, you can use dbSetSpriteFrame to display the appropriate still frame of the animation.
FrankieV
14
Years of Service
User Offline
Joined: 10th Jun 2010
Location:
Posted: 11th Jun 2010 20:23
Wow! You guys are awesome. I should have joined this forum a long time ago, and yes Mireben it is probably the same book. It's "Starting Out with Games and Grapics in C++".
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 13th Jun 2010 08:45
I'm glad to be of help. There is something else I should have told you yesterday about dbPlaySprite. If you use the solution I suggested, to play the animation until frame 4 is reached, this can cause that frame 4 will not be displayed on the screen for enough time, since the animation stops as soon as it is reached. It is not a problem if the last frame remains displayed on the screen, but if you immediately exchange it for another picture (heads or tails sprite) then frame 4 will actually never be visible. THe easiest way to solve this is to include a dummy frame 5 into the animation and use that as a stopping condition. I hope this was useful info as well and post again if you have further questions.

Login to post a reply

Server time is: 2024-11-19 23:18:13
Your offset time is: 2024-11-19 23:18:13