问题描述
我的场景中有 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
方法中,您正在 moving 替换 (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);
}
另外,我认为你的精灵都是相同的大小,所以你无法区分它是一个精灵还是更多.
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
方法中,你应该为每个精灵提供一个标签,然后通过它的 tag 在 ccTouchesMoved
方法中.
In your init
method, you should provide a tag to each sprite, and then modify it by its tag in the ccTouchesMoved
method.
在该方法中,您应该知道正在触摸哪个精灵,然后采取相应的行动.尝试在 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.
当多个精灵被触摸时,您可能需要做一些事情.最常见的做法是对顶部的精灵执行操作(z
)或由精灵tag
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 并且在某些情况下 CCMoveBy.
(1)
To move your sprite(s) to some location, you should be using some CCAction, most likely CCMoveTo and in some cases CCMoveBy.
这篇关于移动一个精灵时所有精灵不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!