所有Sprite在移动一个sprite时不可见 [英] All Sprite not visible while moving one sprite
问题描述
我在我的场景中有7个精灵。所有的精灵都添加到mutablearray。当我触摸一个精灵移动,其他精灵在我的触摸移动方法后不可见
I am having 7 sprites in my scene.All the sprites are add to mutablearray. when i touch one sprite to move,other sprites not visible after my touches move method
这里是我的代码
if( (self=[super init])) {
sprites=[[NSMutableArray alloc]init];
CCLayer *base=[CCSprite spriteWithFile:@"Base.png"];
base.position=ccp(512,384);
[self addChild:base];
x=0;
for(int i=1;i<=7;i++)
{
CCSprite *hole=[CCSprite spriteWithFile:@"ball.png"];
hole.position=ccp(140+x,318);
hole.tag=i;
[self addChild:hole];
hole.visible=YES;
[sprites addObject:hole];
x=x+75;
}
self.isTouchEnabled=YES;
}
return self;
}
My touchesmove method:
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"count:%i",[sprites count]);
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
for(CCSprite *s in sprites)
{
s.position=ccp(location.x,location.y);
}
}
推荐答案
ccTouchesMoved
方法,您将移动取代(1)
:
In your ccTouchesMoved
method, you are moving replacing (1)
all your sprites together in the lines:
for(CCSprite *s in sprites)
{
s.position=ccp(location.x,location.y);
}
此外,我认为你的sprite都是相同的大小,无法区分是一个精灵还是更多。
In addition, I think your sprites are all of the same size so you are unable to differentiate whether it is one sprite or more.
在 init
方法,您应该为每个sprite提供一个标签,然后通过标签。 ccTouchesMoved
In your init
method, you should provide a tag to each sprite, and then modify it by its tag in the ccTouchesMoved
method.
在该方法中,您应该知道正在触摸哪个sprite,然后相应地执行。尝试在 location
周围定义一个矩形。像这个。
In that method, you should know which sprite is being touched, and then act accordingly. Try defining a rectangle around location
. Something like this.
你可能需要做一些事情,当多个sprite被触摸。最常见的做法是对顶部的sprite执行操作( z
)或由sprite 标记
s。
You might have to do something for the case when multiple sprites being touched. The most common thing to do is to perform actions on the sprite at the top (z
) or decide by sprite tag
s.
(1)
某些位置,您应该使用一些 CCAction ,很可能是 CCMoveTo ,在某些情况下< a href =http://www.cocos2d-iphone.org/api-ref/0.99.2/interface_c_c_move_by.html =nofollow> CCMoveBy 。
(1)
To move your sprite(s) to some location, you should be using some CCAction, most likely CCMoveTo and in some cases CCMoveBy.
这篇关于所有Sprite在移动一个sprite时不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!