我的代码:
-(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)];
}
}
此代码使我可以用手指移动图层。很好但是现在我想让用户仅在预定义的CGRect中移动图层。怎么做?
例如:
CGRect rect = CGRectMake(0,0,600,320);
现在应该只允许玩家在该区域内移动图层。在该示例中,他只能将其(通过ipod触摸)左右移动。 (直到600px)。
我需要做出什么改变才能实现这一目标?
感谢您的任何帮助。
祝你今天愉快 :)
最佳答案
保留一个变量以进行检查。
distmoved = distmoved + touchlocation.x - prevlocation.x;
if(distmoved<600)
//move code
注意distmoved是一个float。