A question about timing and key input

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
Imerion
Posts: 200
Joined: Sun Feb 09, 2014 4:42 pm

A question about timing and key input

Post by Imerion »

Hi everyone! My name is Daniel and I'm new here. I recently started learning and using ZGameEditor. After spending a great deal of time looking for a tool which allowed both visual structure as well as pure code for game creation, I found this and really started to like it.
I have since then been working on my first game using ZGameEditor, and will probably hang around on this forum a bit from now on. :)

I ran into a small problem though. My game curently has three states: one for the title screen, one ingame and one for a game over screen. After a certain event, the game switches to the game over screen, where score is displayed. After that the space button is used to continue and switches to the title screen state again. Here comes my problem: I also use the space button to start the game from the title screen. But the key press from the game over screen seems to carry over. So the game instantly jumps to the ingame state and skips the title screen. So my question is, is there any way to make it wait a few seconds so it will stay on the title screen until the player presses space again or can I simply clear all key-presses somehow?

I'm still very much learning this, so any help would be appreciated! Thanks!
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

Hi Daniel, see the attached example. It is maybe not the nicest code, but tries to show the principle: before pressing a key, its state should be unpressed; if it was pressed before, the pressing event is ignored. So you should release the key in meantime to take the action.

AFAIR Kjell provided some more generic code for handling "on release" key events.
Attachments
stateSwitching.zgeproj
example project
(1.58 KiB) Downloaded 407 times
Imerion
Posts: 200
Joined: Sun Feb 09, 2014 4:42 pm

Post by Imerion »

Thanks for the quick help! This should work!

Just for the reference though, is there any way to make the program just wait a few seconds? I thought as an alternative I could just show the game over state for a few seconds then switch to the title automatically. I tried using a timer, but that didn't seem to work. Maybe I can do some kind of DeltaTime loop?
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

Sure, you can use Timer; see the attached example. To be sure to start the timer each time AppState starts, set the CurrentRelativeTime property of Timer to 0.
Attachments
stateSwitching2.zgeproj
example with timers
(1.34 KiB) Downloaded 403 times
Imerion
Posts: 200
Joined: Sun Feb 09, 2014 4:42 pm

Post by Imerion »

Thanks again, that worked great!

I realize the thinking ZGameEditor requires takes a while getting used to. Right now, I'm not sure how to make things happen in the correct order.

I want one of my objects to shrink after a collision. I tried to do this using a ZExpression set to OnUpdate like this :

Code: Select all

int i;

if (AnimateCheck==1)
{
  while(CurrentModel.Scale>0)
  {
    CurrentModel.Scale-=0.01*SmallballHD.DeltaTime;
    @RefreshContent(Component : CurrentModel);
  }
  AnimateCheck=0;
  @RemoveModel();
}

Animatecheck is true after a collision has occured. Still, this does not shrink the object.
Maybe there's another way to achieve this?
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

I'm not sure what effect you want to achieve, either (a) to just shrink your object or (b) smoothly disappear. Try this code for the case (a):

Code: Select all

if(AnimateCheck){
  CurrentModel.Scale -= SHRINK_SPEED * SmallballHD.DeltaTime;
  if(CurrentModel.Scale < TRESHOLD) @RemoveModel();
}
I used constant SHRINK_SPEED for speed of shrinking (try to use 1.0 and if good, remove the constant at all), TRESHOLD used for the smallest possible size of object (can be 0 or some bigger number).

Remark: The code is executed in each rendering cycle, so you should not use loop.

If you want to smoothly remove the object by shrinking, case (b), you can use, e.g., AnimatorSimple in a separate ModelState.
Imerion
Posts: 200
Joined: Sun Feb 09, 2014 4:42 pm

Post by Imerion »

Sorry for my confusing questions, but your help is really appreciated!
ModelStates was the thing I was missing. Using them and your code example, this works perfect now. Thanks again! As soon as the game is done, I'll post it here if you want to see the result. :)
Post Reply