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:????????)]];
}
答案 0 :(得分: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]];
}
答案 1 :(得分: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];
}
我希望这会对你有所帮助。
答案 2 :(得分: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
}
}