0

Cocos2d 新手在这里。我正在构建一个游戏,其中有一个精灵和 5 个按钮展开。我希望精灵移动到我单击的按钮的方向。我有以下代码:

在我的初始化中:

goHere1=[CCMenuItemImage itemWithNormalImage:@"goToBut.png"selectedImage:@"goToBut.png" target:self selector:@selector(imHere:)];
goHere1.position=ccp(70, 650);

然后方法:

- (void) imHere:(id)sender {
    NSLog(@"I'm Here");
    [mole runAction:[CCMoveTo actionWithDuration:1.5 position:????????)]];
}
4

3 回答 3

0

据我所知,您不应该使用moveTo方法来为精灵设置动画。你有没有注意到,当你使用“moveto”时,你的移动并不顺畅?

我是这样做的,可能并不完美。

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [zoombase convertTouchToNodeSpace:myTouch];
    self.destinationLocation = loaction;
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *myTouch = [touches anyObject];
    CGPoint location = [zoombase convertTouchToNodeSpace:myTouch];
    self.destinationLocation = loaction;
}

-(void)update:(ccTime)dt {

    //that will do until your sprite reach you destination point
    if(!CGPointEqualToPoint(self.spriteToMove,self.destinationLocation)) {
        CGPoint *stepToMove = ccp(0.2/destinationLocation,0.2/destinationLocation); //some piece of orginal destination
        [self.spriteToMove setPosition:ccpAdd(stepToMove,self.spriteToMove.position)]; // add that pice to your sprite current location
    }
}
于 2013-01-24T09:53:50.233 回答
0

试试这个代码

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

 self.moveaction=[CCSequence actions:[CCMoveTo actionWithDuration:0.5f position:touchLocation],[CCCallFunc actionWithTarget:self selector:@selector(moveend)],nil];
[sprite runAction:moveaction];
 } 

我希望这能帮到您。

于 2013-01-24T06:18:02.280 回答
0

您作为选择器操作传递的方法,您CCMenuItemImage可以使用method 参数CCMenuItemImage触发该方法。sender因此,您可以使用((CCMenuItemImage*)sender).position(需要将sender变量转换为CCMenuItemImage第一个)来获得位置

- (void) imHere:(id)sender {
    NSLog(@"I'm Here");
    [mole runAction:[CCMoveTo actionWithDuration:1.5 position:((CCMenuItemImage*)sender).position]];
}
于 2013-01-24T03:54:33.943 回答