4

I made a "game over" text appears at the end of the game and added addeventlistener to the self.stage on touch. when this touched, it should execute the event function which loads the first screen of the game. however, on the first screen I have in the middle of the screen click to play text which has its own touch event.

In the simulator, if I click in the middle of the screen on the game over screen, it automatically also record event for click to play and the game starts.

-I don't think the eventhandler of click to play sticks around, because I use [self removeAllChildren] when game starts. also, I tested during game play by clicking in the same area and correctly no touch event records for click to play.

Edit:

I made a workaround for this, but not sure if that's an optimal solution:

I made the onevent touch start after an interval of time using NSTimer

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(attachEventToStart:) userInfo:startTextField repeats:NO];

then in attachEventToStart:

-(void)attachEventToStart:(NSTimer *)theTimer
{
    SPTextField *startTextField = [theTimer userInfo];
    [startTextField addEventListener:@selector(gameStartOnTouch:) atObject:self forType:SP_EVENT_TYPE_TOUCH];
}

this worked, if anyone have a better solution would be great

iDev
  • 23,310
  • 7
  • 60
  • 85
Dreaded semicolon
  • 2,274
  • 1
  • 28
  • 43

2 Answers2

2

I found better alternative than the timer.

I need to use SPTouchPhaseEnded , so I added the following lines to the event callback,

SPTouch *touchEnded = [[event touchesWithTarget:self andPhase:SPTouchPhaseEnded] anyObject];
if (!touchEnded)
     return;
SOME GAME START CODE HERE...

Now the action only happens when touch event ends, therefore it will not be carried out to the next screen. so this works like on key_up for those who are familiar with html-JS or Visual studio

Make sure the callback parameter is of type SPTouchEvent not SPEvent.

I found the answer here: http://forum.sparrow-framework.org/topic/event-listeners-getting-called-too-fast

Dreaded semicolon
  • 2,274
  • 1
  • 28
  • 43
1

Though I am not sure of the exact structure or implementation of your views, I think I have an alternative to your solution. Instead of setting a delay on the touch for the start game event, why not disable it when your "game over" text is being displayed? In my experience, this has worked better for me, and has been more self-documenting.

EmphaticArmPump
  • 774
  • 4
  • 7
  • 23
  • I'm already removing all objects and that should remove touch events isn't it? it seems to me it's happening because after you touch the game over screen , the start screen load which loads the touch event for click to play, then touch event also received by the click to play if there is no delay. is there touch_up similar to key_up event maybe I'm trapping the wrong touch event. – Dreaded semicolon Aug 23 '11 at 06:42
  • So you are saying that the game over screen is not being added on top of the start game screen, but rather that the start screen has been removed from the view, and the game over screen has been added to the view. Is that correct? Can you please post some additional code from when this problem did occur? I'm sure we can figure it out. – EmphaticArmPump Aug 23 '11 at 15:22
  • Yes except first start screen, then game play, then game over screen, then the start screen again. I remove them by [self removeAllChildren] on each screen. I ll post more code when I get to my mac. – Dreaded semicolon Aug 23 '11 at 18:05