Ios 移动一个精灵时,所有精灵均不可见
我的场景中有7个精灵。所有精灵都添加到可变数组中。当我触摸一个精灵移动时,其他精灵在触摸移动方法后不可见 这是我的密码Ios 移动一个精灵时,所有精灵均不可见,ios,xcode,cocoa-touch,cocos2d-iphone,Ios,Xcode,Cocoa Touch,Cocos2d Iphone,我的场景中有7个精灵。所有精灵都添加到可变数组中。当我触摸一个精灵移动时,其他精灵在触摸移动方法后不可见 这是我的密码 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;
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);
}
}
if((self=[super init])){
sprites=[[NSMutableArray alloc]init];
CCLayer*base=[CCSprite spriteWithFile:@“base.png”];
基本位置=ccp(512384);
[self addChild:base];
x=0;
对于(inti=1;i您在cctouchsmoved中的代码将所有精灵移动到一个单触位置,因此您只能看到一个精灵,而其他精灵实际上都堆叠在下面
如果您想要实现的是在触摸时简单地拖动精灵,则需要测试触摸位置和CCTouchStart中每个精灵的边界框之间的交点。一旦循环找到位于触摸下方的精灵,您将保存对该精灵的引用,并在ccTouchMoved中转换该精灵的位置s位置以及自上次调用ccTouchMoved以来移动的金额
查看Ray Wenderlich的教程:在您的ccTouchesMoved
方法中,您正在移动并替换(1)
所有精灵,如下所示:
for(CCSprite *s in sprites)
{
s.position=ccp(location.x,location.y);
}
此外,我认为您的精灵都是相同大小的,因此您无法区分它是一个精灵还是多个精灵
在您的init
方法中,您应该为每个精灵提供一个标记,然后在ccTouchesMoved
方法中通过其进行修改
在该方法中,您应该知道正在触摸哪个精灵,然后采取相应的行动
在触摸多个精灵的情况下,您可能必须执行一些操作。最常见的操作是对顶部的精灵执行操作(z
)或由精灵标记决定
(1)
若要将您的精灵移动到某个位置,您应该使用一些,在某些情况下,最有可能是。+1用于解决确切问题的链接。我想将精灵拖到不移动的位置。您能帮我编写代码吗。@Kentoh的答案链接是现成的资源,at code位于页面末尾。