我的代码:
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint prevLocation = [touch previousLocationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];
CGPoint diff = ccpSub(touchLocation,prevLocation);
[self setPosition: ccpAdd(self.position, diff)];
}
}
这段代码让我可以用手指 move 图层。效果很好。但现在我想让用户只 move 预定义的 CGRect 内的图层。如何做到这一点?
例如:
CGRect rect = CGRectMake(0,0,600,320);
现在应该只允许玩家 move 该矩形内的图层。在该示例中,他只能(在 iPod touch 上)向左和向右 move 它。 (直到 600 像素)。
我需要改变什么才能实现这一目标?
感谢您的帮助。 祝你有美好的一天:)
最佳答案
保留一个变量来检查..例如..
distmoved = distmoved + touchlocation.x - prevlocation.x;
if(distmoved<600)
//move code
请注意,distmoved 是一个 float ..
关于iphone - 使用 "ccTouchesMoved" move CCLayer 可以工作,但需要一些调整,我不明白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5236249/