开发者

IOS Sparrow Framework: how to prevent double touch

开发者 https://www.devze.com 2023-03-28 01:15 出处:网络
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

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
{
    SPTex开发者_Python百科tField *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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消