【问题标题】:How do you use parameters for methods in CCMenuitem's selector如何在 CCMenuitem 的选择器中为方法使用参数
【发布时间】:2012-08-30 01:05:28
【问题描述】:
-(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(move:converted location),但它似乎无法接受参数,是否有一些这样做可以传递参数。

【问题讨论】:

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


    【解决方案1】:

    我认为没有办法以这种方式传递参数。有很多方法可以做你想做的事。这就是我会做的:

    首先在实现中添加一个实例变量,例如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]];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      相关资源
      最近更新 更多