Objective c 如何在多个位置动态放置同一个精灵?
如何在多个位置动态放置同一个精灵?我已经看到了另一个问题,但是,你只能用三个精灵来回答。我想有一个动态的精灵数量。我的目标是,我要让它射出三颗或更多的子弹,而不是只射出一颗子弹。我已经完成了所有的数学运算,但是,我需要把三个精灵画成一个for循环。这是我到目前为止所拥有的Objective c 如何在多个位置动态放置同一个精灵?,objective-c,ios,cocos2d-iphone,sprite,Objective C,Ios,Cocos2d Iphone,Sprite,如何在多个位置动态放置同一个精灵?我已经看到了另一个问题,但是,你只能用三个精灵来回答。我想有一个动态的精灵数量。我的目标是,我要让它射出三颗或更多的子弹,而不是只射出一颗子弹。我已经完成了所有的数学运算,但是,我需要把三个精灵画成一个for循环。这是我到目前为止所拥有的 - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = [touches anyObject];
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint pointOne = [touch locationInView:[touch view]];
CGSize size = [[CCDirector sharedDirector] winSize];
CGPoint position = turret.position;
CGFloat degrees = angleBetweenLinesInDegrees(position, pointOne);
turret.rotation = degrees;
pointOne.y = size.height-pointOne.y;
CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"];
projectile.position = turret.position;
// Determine offset of location to projectile
int angle = angleBetweenLinesInDegrees(position, pointOne);
int startAngle = angle-15;
int shots = 3;
NSMutableArray *projectiles = [[NSMutableArray alloc] initWithCapacity:shots];
// Ok to add now - we've double checked position
for(int i = 0;i<shots;i++) {
[self addChild:projectile z:1];
int angleToShoot = angle;
int x = size.width;
int y = x*tan(angle);
CGPoint realDest = ccp(x,y);
projectile.tag = 2;
if (paused==0 ) {
[_projectiles addObject:projectile];
// Move projectile to actual endpoint
[projectile runAction:
[CCSequence actions:
[CCMoveTo actionWithDuration:1 position:realDest],
[CCCallBlockN actionWithBlock:^(CCNode *node) {
[_projectiles removeObject:node];
[node removeFromParentAndCleanup:YES];
}],
nil]];
}
}
}
-(void)cctouchesend:(NSSet*)接触事件:(UIEvent*)事件{
UITouch*touch=[触摸任何对象];
CGPoint pointOne=[触摸位置视图:[触摸视图]];
CGSize size=[[CCDirector sharedDirector]winSize];
CGPoint位置=炮塔位置;
CGFloat degrees=直线之间的角度角度角度角度(位置,点1);
转塔旋转=度;
pointOne.y=大小.高度pointOne.y;
CCSprite*Sprojection=[CCSprite spriteWithFile:@“sprojecte.png”];
射弹位置=炮塔位置;
//确定位置对射弹的偏移量
int angle=线与度之间的角度(位置,点1);
int startAngle=角度-15;
int-shots=3;
NSMutableArray*射弹=[[NSMutableArray alloc]initWithCapacity:shots];
//现在可以添加了-我们已经仔细检查了位置
对于(inti=0;i,您需要创建3个不同的精灵,并将它们全部作为子对象添加。
通常,对于这样的事情,最好使用CCBatchNode(查看cocos文档)。
使用batchnode,您可以在1个draw调用中绘制所有子对象,唯一的约束是,batchnode的所有子对象都需要在同一精神表上具有纹理(或者在您的情况下,如果它们具有相同的“文件名”)
对于3个投射物,你不会有性能问题,但这是正确的设计方法,如果你需要在屏幕上有几十个投射物,而不使用batchnode,游戏将无法顺利运行
重述:
创建一个ccbatchnode,
将batchnode添加为self的子节点(我假设它的ur层或主节点)
创建3个精灵,并将其作为batchnode的子对象添加,这样就不可能动态地执行此操作了?我不明白您所说的动态。如果您希望3个精灵同时出现在屏幕上,则需要3个对象。您试图将同一对象作为子对象多次添加,这就是为什么会出现此异常。我需要一个特定的值t、 它不是一个固定的数字,比如三,它可以是1,2,3,4,5,6等等。