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 / Display game loop problem

Author
Message
hhkk1712
13
Years of Service
User Offline
Joined: 11th May 2011
Location:
Posted: 12th May 2011 06:59
Hello

I got problem with my main game loop. My game is a puzzle game which look like bejeweled, just change diamond to the number. My problem is that I cannot move the number down to the board when the game start. I know my problem is because of the while (LoopGDK()). It could not update the motion. The sprite just appear and move a little then stop. When I try to not use the game() function and call the spriteDown() function directly in the while (LoopGDK()), it works perfectly. However, if I choose that way, I cannot create the game menu and control the game state. Someone please help me

Here is my code



Here is my problem




And here is what I want





Please help me. Thanks a lot
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 12th May 2011 08:24
What's this command doing at the end of your gamePlay function?


That loads a bitmap onto the screen. If this bitmap exists and loaded successfully, it may cover everything that the program has drawn onto the screen before, so you will actually see a static picture. It's just a guess but try to comment this line out and see if it helps. In any case it's not good to re-load a bitmap every loop.
hhkk1712
13
Years of Service
User Offline
Joined: 11th May 2011
Location:
Posted: 12th May 2011 08:56
It just the same. I use bitmap because something wrong when I load the image in regular way. It doesn't matter anyway. My problem is my number stuck at the beginning. It seem like the game doesn't update it. I try everything but cannot solve it (
hhkk1712
13
Years of Service
User Offline
Joined: 11th May 2011
Location:
Posted: 12th May 2011 09:03
Here is the separate code I test which run perfectly. The only difference is it call spriteDown() directly in the main while loop. But I don't want to use this way because I will not able to control the game state and make game menu

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 12th May 2011 19:57
I think I've got it. The difference is that in the test code you call printBoard once, while in the other code you call it every loop. The printBoard function not only re-loads the images unnecessarily but also resets the sprites at the default coordinates (dbSprite command). Comment out this line in the gamePlay function:

hhkk1712
13
Years of Service
User Offline
Joined: 11th May 2011
Location:
Posted: 13th May 2011 04:48
Great, it works. I still have to adjust the code a little bit but it works anyways. Thanks a lot
hhkk1712
13
Years of Service
User Offline
Joined: 11th May 2011
Location:
Posted: 14th May 2011 03:06
I have another problem

I want to get the coordinate value of two consecutive mouse clicks, then handle it for my game purpose. However, when I test it, it show the number just for a short time and then go back to zero. It means whenever I click and hold the mouse button, it show the number, but when i pull off, it goes back to zero. I think maybe I got problem again with the main while loop. I don't really understand how the while loopDGK work. Could you explain me more about the main while loop of dark gdk. thanks.

Here is my function code. I just try to get the coordinate of the mouse click, then write them as text to the screen for testing. But whenever I click off the point, the value go back to zero

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 15th May 2011 15:17
dbMouseClick doesn't mean that the mouse was clicked (where "click" means the complete action of press and then release). It means that the mouse button is held down at the moment. That function should be actually called dbMouseDown and not dbMouseClick, it's not correctly named. So it's normal that you don't read any coordinates when the button is released. It also follows that if you rely on dbMouseClick alone, you will have many more mouseclick events than you need, because dbMouseClick returns 1 as long as the button is held down, and the duration of a mouseclick is longer than one game loop. (E.g. if the game runs at 60 frames per second, and you hold the mouse button down for half a second, there will be 30 dbMouseClick events.)

You can't handle two consecutive mouseclicks in one game loop for the same reason: a click is longer than a loop.

If you want to detect one click, the usual solution is to introduce a boolean variable that stores the status of the mouse button in the last loop, and process the click only at the moment when the button status changes from "down" to "up". Something like this:



where mouseDown is a boolean variable, but you must make it either static (if it's in a function) or global, or a variable inside a class or struct, since a simple local variable in a function won't preserve its status throughout several function calls.

Since everything happens in loops, you need to get used to thinking in statuses and that's not always easy. If you need to process two consecutive mouse clicks together, then you also need to store the coordinates of the previous click, and the variables holding the coordinates must be static as well.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 15th May 2011 15:19
P.S. I don't quite see the reason of involving dbMouseMoveX/Y into testing for clicks, since those functions tell you the mouse movement in the last loop. That is usually used for dragging, not click handling.

Login to post a reply

Server time is: 2024-10-02 17:35:19
Your offset time is: 2024-10-02 17:35:19