如何在CCMenuitem的选择器中使用方法的参数

时间:2012-08-30 01:05:28

标签: objective-c parameters cocos2d-iphone parameter-passing

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
    CGPoint convertedlocation = [[CCDirector sharedDirector] convertToGL:location];  
    bb=[CCMenuItemImage itemFromNormalImage:@"Aqua-ball.png" selectedImage:@"Aqua-ball.png" target:self selector:@selector(move:)];
    menu1=[CCMenu menuWithItems:bb, nil];

    //ignore this.....

}

-(void) move:(CGPoint) touch{
    [character runAction:[CCMoveTo actionWithDuration:1 position:ccp(touch.x,touch.y)]];
}

我试图制作CCmenuitem选择器:@selector(移动:转换位置),但它似乎不能接受参数,是否有一些这样做我可以传递参数。< / p>

1 个答案:

答案 0 :(得分:0)

我认为没有办法以这种方式传递参数。尽管如此,有很多方法可以做你想做的事情。这就是我要做的事情:

首先向实现添加一个实例变量,例如CGPoint lastTouchLocation。然后像这样使用它:

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
    CGPoint convertedlocation = [[CCDirector sharedDirector] convertToGL:location];

    lastTouchLocation = convertedLocation;
    bb=[CCMenuItemImage itemFromNormalImage:@"Aqua-ball.png" selectedImage:@"Aqua-ball.png" target:self selector:@selector(buttonTapped)];

    menu1=[CCMenu menuWithItems:bb, nil];    
}

-(void) buttonTapped {
    [character runAction:[CCMoveTo actionWithDuration:1 position:lastTouchLocation]];
}