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アクセスできます。したがって、次を使用して位置を取得できます(変数を最初にキャストする必要があります)CCMenuItemImagesender((CCMenuItemImage*)sender).positionsenderCCMenuItemImage

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