开发者

While I touch the screen my sprite has to change

开发者 https://www.devze.com 2023-02-22 06:30 出处:网络
I am trying to develope an action and I can not get this efect: while I touch the screen my sprite has to change for a开发者_JS百科nother one and when I hang off it has to go back to the initial stat

I am trying to develope an action and I can not get this efect:

while I touch the screen my sprite has to change for a开发者_JS百科nother one and when I hang off it has to go back to the initial state. How can I detect the time i am touching the screen and make the sprite changes?

thanks for your help.


Create a subclass of CCNode with variables normalSprite and pressedSprite. In the initialiser, add this so that it handles touches:

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

Add a method with this signature to handle when the screen is touched:

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    // Check that touch is within boundaries of this object
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
    if (!CGRectContainsPoint(CGRectMake(0, 0, self.boundingBox.size.width, self.boundingBox.size.height), touchLocation)) {
        return TRUE;
    }
    // Switch image
    [self removeChild:normalSprite cleanup:NO];
    [self addChild:pressedSprite];

    return TRUE;
}

Add a method with this signature to handle when the finger goes off:

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    // Switch image back
    [self removeChild:pressedSprite cleanup:NO];
    [self addChild:normalSprite];
}

You can also add ccTouchMoved (everything else the same as ccTouchEnded) to handle situations where the touch moves outside and back inside the boundaries of the object.

0

精彩评论

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

关注公众号