Iphone 所有相同精灵上的ccTouch事件(精灵阵列)
我刚刚从一个教程开始学习Cocos2d。我想随机飞行“蝙蝠”,当我触摸屏幕上的任何地方时,所有聚集在我触摸位置的蝙蝠都会随机飞行。Iphone 所有相同精灵上的ccTouch事件(精灵阵列),iphone,xcode4,cocos2d-iphone,touch,Iphone,Xcode4,Cocos2d Iphone,Touch,我刚刚从一个教程开始学习Cocos2d。我想随机飞行“蝙蝠”,当我触摸屏幕上的任何地方时,所有聚集在我触摸位置的蝙蝠都会随机飞行。 这是我的密码: -(id) init { if( (self=[super init])) { [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Bat.plist"]; //Bats Array Initialization bats = [[N
这是我的密码:
-(id) init
{
if( (self=[super init])) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Bat.plist"];
//Bats Array Initialization
bats = [[NSMutableArray alloc] init];
//Add bats using a batch node.
CCSpriteBatchNode *batch1 = [CCSpriteBatchNode batchNodeWithFile:@"Bat.png" capacity:10];
[self addChild:batch1 z:2 tag:TAG_BATS];
//Make them start flying up.
for(int x=0; x<5; x++){
//Create SimpleAnimObject of bat
bat1 = [SimpleAnimObj spriteWithBatchNode:batch1 rect:CGRectMake(0,0,48,48)];
[batch1 addChild:bat1];
[bat1 setPosition:ccp(arc4random()%900+40, arc4random()%600+50)];
[bat1 setScale:0.6f];
float flappingSpeed = [self makeBatFlyUp:bat1];
bat1.velocity = ccp((arc4random()%1000)/500 + 0.2f, 0.1f/flappingSpeed);
[bats addObject:[NSValue valueWithPointer:bat1]];
[bat1 retain];
//Set the bat's direction based on x velocity.
if(bat1.velocity.x > 0){
bat1.flipX = YES;
}
}
self.isTouchEnabled = YES;
[self schedule:@selector(step:)];
}
return self;
}
-(float)makeBatFlyUp:(SimpleAnimObj*)bat {
CCSpriteFrameCache * cache = [CCSpriteFrameCache sharedSpriteFrameCache];
float delay = (float)(arc4random()%5+5)/150;
CCAnimation *animation = [[CCAnimation alloc] initWithName:@"simply_bat_fly" delay:delay];
int num = arc4random()%9+1;
for(int i=1; i<=9; i+=1){
[animation addFrame:[cache spriteFrameByName:[NSString stringWithFormat:@"b%i.tiff",num]]];
num++;
if(num > 9){ num = 1; }
}
[bat stopAllActions];
[bat runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation]]];
bat.animationType = BAT_FLYING_UP;
return delay;
}
蝙蝠飞行正常,但当我在屏幕上触摸时,只有一只蝙蝠跟随我的触摸事件,其他蝙蝠则继续随机移动。有人能帮我吗?如果我错过了什么,或者我错在哪里
谢谢 对正在使用的bat只有一个引用:
bat1
变量。您需要使用bats阵列来访问所有bats
另外,去掉
[bat1 retain]
行,因为通过将每个bat添加到bats数组中,它已经被保留了。当您通过调用addChild:node
将Cocos节点作为父节点的子节点时,Cocos节点也会被保留。您对正在使用的bat只有一个引用:bat1
变量。您需要使用bats阵列来访问所有bats
另外,去掉
[bat1 retain]
行,因为通过将每个bat添加到bats数组中,它已经被保留了。当您通过调用addChild:node
将Cocos节点设置为父节点的子节点时,它们也会被保留。。首先谢谢你,老兄。。!!您能告诉我一些想法吗?“我如何使用bats
数组访问所有的bats?”任何示例代码或提示都会非常有帮助!!你需要知道你想要解决哪一个bat。然后通过[bats objectAtIndex:0]获取蝙蝠(这将返回第一个蝙蝠)。但我希望所有蝙蝠(所有蝙蝠都处于随机位置)跟随我的触摸位置。我对Cocos2d完全是新手,所以我真的不知道——这可能吗?如果是的话,那么你们能在编码方面给我更多的指导吗?我明白了。。首先谢谢你,老兄。。!!您能告诉我一些想法吗?“我如何使用bats
数组访问所有的bats?”任何示例代码或提示都会非常有帮助!!你需要知道你想要解决哪一个bat。然后通过[bats objectAtIndex:0]获取蝙蝠(这将返回第一个蝙蝠)。但我希望所有蝙蝠(所有蝙蝠都处于随机位置)跟随我的触摸位置。我对Cocos2d完全是新手,所以我真的不知道——这可能吗?如果是的话,你们能在编码方面给我更多的指导吗?
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
float velocityBat = 1024/3.0 ;
CGPoint moveDifference = ccpSub(touchLocation,bat1.position );
float distanceMove = ccpLength(moveDifference);
float moveduration = distanceMove / velocityBat;
// here i have to use Array but i don't know how to use array to access all same sprites.
self.moveAction = [CCSequence actions:
[CCMoveTo actionWithDuration:moveduration position:touchLocation],
[CCCallFunc actionWithTarget:self selector:@selector(bearMoveEnded)],
nil
];
[bat1 runAction:moveAction];
return YES;
}