所以我一直有一些問題與cctouchesEnded當一觸之間結束它觸發了所有我的按鈕結束行動。我明白爲什麼會發生這種情況,但爲了解決這個問題,我不確定要做什麼。如果我要爲按鈕創建一個類併爲每個新按鈕創建一個新類的實例,那麼每個按鈕都將是分開的嗎?如果是的話,有沒有一個很好的例子來說明如何使用類在cocos2d中創建簡單的按鈕?任何幫助,將不勝感激。這是我目前如何使用按鈕。ccTouchesEnded已結束所有操作
-(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
for(UITouch *touch in [event allTouches]) {
CGPoint loc = [touch locationInView: [touch view]];
loc = [[CCDirector sharedDirector] convertToGL:loc];
if (CGRectContainsPoint(button2.boundingBox, loc)) { //Left button pressed
[self.character stopAction:self.walkAction];
[self.character setScaleX:-2];
isLeft = YES;
isRight = NO;
right = NO;
left = YES;
if(!isRunning && !isJumping && !isSpin)
{
// [self.character stopAction:self.jumpAction];
[self.character runAction:self.runAction];
isRunning = YES;
}
else if(isSpin)
{
[self.character stopAction:self.runAction];
[self.character stopAction:self.walkAction];
[self.character stopAction:self.jumpAction];
isRunning = NO;
isStanding = NO;
}
}
if (CGRectContainsPoint(button.boundingBox, loc)) { //Right button pressed
[self.character stopAction:self.walkAction];
[self.character setScaleX:2];
isLeft = NO;
isRight = YES;
right = YES;
left = NO;
if(!isRunning && !isJumping && !isSpin)
{
[self.character stopAction:self.jumpAction];
[self.character runAction:self.runAction];
isRunning = YES;
isStanding = NO;
}
else if(isSpin)
{
[self.character stopAction:self.runAction];
[self.character stopAction:self.walkAction];
[self.character stopAction:self.jumpAction];
isRunning = NO;
isStanding = NO;
}
}
}
}
-(void)ccTouchesMoved:(NSSet *)touchesT withEvent:(UIEvent *)event
{
NSMutableArray *touches = [NSMutableArray arrayWithCapacity:100];
for (UITouch *tmpTouch in [event allTouches]) {
[touches addObject:tmpTouch];
}
CGPoint locOne = [[touches objectAtIndex:0] locationInView: [[touches objectAtIndex:0] view]];
locOne = [[CCDirector sharedDirector] convertToGL:locOne];
CGPoint locTwo = ccp(-100, -100);
if ([touches count] > 1) {
locTwo = [[touches objectAtIndex:1] locationInView: [[touches objectAtIndex:1] view]];
locTwo = [[CCDirector sharedDirector] convertToGL:locTwo];
}
CGPoint locThree = ccp(-100, -100);
if ([touches count] > 2) {
locThree = [[touches objectAtIndex:2] locationInView: [[touches objectAtIndex:2] view]];
locThree = [[CCDirector sharedDirector] convertToGL:locThree];
}
if(!(((CGRectContainsPoint(button2.boundingBox, locOne) || CGRectContainsPoint(button2.boundingBox, locTwo) || CGRectContainsPoint(button2.boundingBox, locThree)))|| ((CGRectContainsPoint(button.boundingBox, locOne) || CGRectContainsPoint(button.boundingBox, locTwo) || CGRectContainsPoint(button.boundingBox, locThree)))))
{
if(!isJumping && !isSpin && !isStanding){
[self.character stopAction:self.jumpAction]; //No buttons
[self.character stopAction:self.runAction];
[self.character runAction:self.walkAction];
isRunning = NO;
isStanding = YES;
}
}
if (!(CGRectContainsPoint(button2.boundingBox, locOne) || CGRectContainsPoint(button2.boundingBox, locTwo) || CGRectContainsPoint(button2.boundingBox, locThree))) {
left = NO;
} else { //Left button
[self.character setScaleX:-2];
left = YES;
isLeft = YES;
isRight = NO;
if(!isRunning && !isJumping && !isSpin && !isShooting)
{
[self.character stopAction:self.jumpAction];
[self.character stopAction:self.walkAction];
[self.character runAction:self.runAction];
isRunning = YES;
isStanding = NO;
}
else if(isSpin)
{
[self.character stopAction:self.runAction];
[self.character stopAction:self.walkAction];
[self.character stopAction:self.jumpAction];
isRunning = NO;
isStanding = NO;
}
}
if (!(CGRectContainsPoint(button.boundingBox, locOne) || CGRectContainsPoint(button.boundingBox, locTwo) || CGRectContainsPoint(button.boundingBox, locThree))) {
right = NO;
} else {
[self.character setScaleX:2]; //Right button
right = YES;
isRight = YES;
isLeft = NO;
if(!isRunning && !isJumping && !isSpin)
{
[self.character stopAction:self.jumpAction];
[self.character stopAction:self.walkAction];
[self.character runAction:self.runAction];
isRunning = YES;
isStanding = NO;
}
else if(isSpin)
{
[self.character stopAction:self.runAction];
[self.character stopAction:self.walkAction];
[self.character stopAction:self.jumpAction];
isRunning = NO;
isStanding = NO;
}
}
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
right = NO;
left = NO; //Touch ended
isRunning = NO;
if(!isJumping){
[self.character stopAction:self.runAction];
[self.character stopAction:self.jumpAction];
if(isStanding && !isSpin){
[self.character stopAction:self.walkAction];
isStanding = NO;
}
if(!isStanding && !isSpin){
[self.character stopAction:self.runAction];
[self.character runAction:self.walkAction];
isStanding = YES;
}
}
}
一個很好的例子是CCMenuItem沒有?雖然我不明白爲什麼你不能在這種情況下使用CCMenu和三個CCMenuItems ...你有源代碼...應該很容易擴展一個類,或者創建一個擁有良好起點的自己的CCMenu。 – YvesLeBorg