2013-01-24 88 views
0

Cocos2d新手在這裏。我正在構建一個遊戲,其中有一個精靈和5個按鈕展開。我想讓精靈移動到我點擊的按鈕的方向。我有以下代碼:Cocos2d sprite MoveTo單擊按鈕?

在我的初始化:

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:????????)]]; 
} 

回答

0

您爲選擇操作爲您CCMenuItemImage可以訪問哪些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]]; 
} 
+0

我想到了太多,但我得到以下錯誤:「物業‘位置’的類型‘ID’的對象找不到」 – Figgy

+0

鑄'sender'到'CCMenuItemImage *'(編輯) – Lukman

+0

它的工作...非常感謝!!!!!! – Figgy

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]; 
} 

我希望這會幫助你。

0

因爲我知道你不應該使用moveTo方法來動畫sprite。你沒有注意到當你使用'移動'時,你的移動不順利嗎?

我這樣做,也許不完美。

- (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 
    } 
}