cocos2d Move Sprite using buttons

420 Views Asked by At

Using CCMenu I have create Two Buttons Up and Down Here is the code

    CCSprite *normlUp = [CCSprite spriteWithFile:@"Up.png"];
    CCSprite *selectedUp = [CCSprite spriteWithFile:@"Up.png"];
    selectedUp.color = ccGREEN;

    CCMenuItemSprite *up = [CCMenuItemSprite itemFromNormalSprite:normlUp selectedSprite:selectedUp target:self selector:@selector(upItemTouched)];
    up.position = CGPointMake(-220, -115);

    CCSprite *normlDown = [CCSprite spriteWithFile:@"Down.jpeg"];
    CCSprite *selectedDown = [CCSprite spriteWithFile:@"Down.jpeg"];
    selectedDown.color = ccGREEN;

    CCMenuItemSprite *down = [CCMenuItemSprite itemFromNormalSprite:normlDown selectedSprite:selectedDown target:self selector:@selector(downItemTouched)];
    down.position = CGPointMake(-220,-140 );

    CCMenu *upDown = [CCMenu menuWithItems:up,down,nil];
    [self addChild:upDown z:4];

How to write upItemTouched and downItemTouched Methods
Also the sprite should move smoothly on the screen
I am New to cocos2d so please accept my simple questions........

1

There are 1 best solutions below

0
On

Use CCMenuItemSprite assigning adding selector to the sprite, so when you touch down/up sprite that selector method will be called. so, when "up" is touched, set one boolean to true and when "down" is touched, set another boolean to true.

Now in update/tick method check which boolean in true and then move the sprite.

When done moving, in touchesEnded method just set those boolean to false.