我正在实现一种移动精灵的方法,我在放置所有精灵的地方放置了一个数组,并且我想为该数组提供触摸移动的动作。但是当我运行它时,什么也没有发生:
-(void)onEnter
{
[super onEnter];
self.isTouchEnabled = YES;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
mSpriteOnHand = nil;
for(CCSprite *cloth in mSpriteArray)
{
if(CGRectContainsPoint([cloth boundingBox], location))
{
mSpriteOnHand = cloth;
break;
}
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if(mSpriteOnHand)
mSpriteOnHand.position = location;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
mSpriteOnHand = nil;
}
-(void)onExit
{
[mSpriteArray release];
mSpriteArray = nil;
[super onExit];
}
有什么不对?我认为问题出在 Touch Began 上,但我不确定...
编辑:
我的精灵没有响应,但方法似乎没问题。这是我的初始化:
-(id) init
{
if( (self=[super init]) )
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite * backGround = [CCSprite spriteWithFile:@"background_ipad.png"];
backGround.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:backGround z:0];
cloth1 = [CCSprite spriteWithFile:@"clothe_1.png"];
cloth1.position = ccp(380, 500);
[self addChild:cloth1 z:1];
cloth2 = [CCSprite spriteWithFile:@"clothe_2.png"];
cloth2.position = ccp(530, 500);
[self addChild:cloth2 z:2];
cloth3 = [CCSprite spriteWithFile:@"clothe_3.png"];
cloth3.position = ccp(700, 500);
[self addChild:cloth3 z:3];
cloth4 = [CCSprite spriteWithFile:@"clothe_4.png"];
cloth4.position = ccp(380, 270);
[self addChild:cloth4 z:4];
cloth5 = [CCSprite spriteWithFile:@"clothe_5.png"];
cloth5.position = ccp(530, 270);
[self addChild:cloth5 z:5];
cloth6 = [CCSprite spriteWithFile:@"clothe_6.png"];
cloth6.position = ccp(700, 270);
[self addChild:cloth6 z:6];
[mSpriteArray addObject: cloth1];
[mSpriteArray addObject: cloth2];
[mSpriteArray addObject: cloth3];
[mSpriteArray addObject: cloth4];
[mSpriteArray addObject: cloth5];
[mSpriteArray addObject: cloth6];
}
return self;
}