问题描述
我的场景中有 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)或由精灵tags 决定.
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 tags.
(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.
这篇关于移动一个精灵时所有精灵不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)