【发布时间】:2011-06-09 15:27:00
【问题描述】:
请原谅我的基本问题,因为我对 cocos2d 还很陌生,但是我在移动精灵时遇到了问题。该方法似乎可以满足我的要求……只要它沿正 x 和正 y 方向(右上角)移动。如果我尝试将其向下或向左移动,则不起作用。
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:[myTouch view]];
point = [[CCDirector sharedDirector] convertToGL:point]; //create point location
CCNode *sprite = [self getChildByTag:kTagSprite]; //set sprite
CGRect spriteRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.contentSize.width, sprite.contentSize.height); //create box around sprite and get position
if(CGRectContainsPoint(spriteRect, point)){
[sprite setPosition:point]; //if my pointer is inside of the sprite box, move the sprite
}
}
谢谢。
编辑:添加我的初始化和标记参考
-(id) init{
if((self = [super init])){
self.isTouchEnabled = YES;
CCSprite *mySprite = [CCSprite spriteWithFile:@"sprite.png"];
mySprite.position = ccp(240, 40);
[self addChild:mySprite z:0 tag:kTagSprite];
}
return self;
}
我在以下位置声明了 kTagSprite:
enum{
kTagSprite
};
【问题讨论】:
-
你是如何在 kTagSprite 后面创建对象的?向我们展示代码..如何在 init 中加载精灵?
-
问题已编辑。感谢收看。
标签: cocos2d-iphone touch cgrect