iphone - 使用 "ccTouchesMoved" move CCLayer 可以工作,但需要一些调整,我不明白

标签 iphone cocos2d-iphone move layer

我的代码:

-(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/

相关文章:

ios - FacebookSDK 是否始终在共享或登录后、返回当前屏幕之前显示 LaunchScreen?

iphone - cocos2d + 加速度计问题

javascript - 如何在kineticjs中 move 图像

powershell - move 带有文件的文件夹(异常(exception))

iphone - 在 iOS sdk 中使用 ZipArchive 解压缩文件

ios - iPad Air iOS 7.1 和 iOS 8.0 中的 FFCalender + Xcode 6.0.1 + UICollectionView,月 View 分散

ios - 使用 theos tweak 在收到的短信上启动应用程序

iphone - 带有 ARC 的新 iPhone cocos2d 项目

swift : Trouble Exporting Database

C# 检测文件在不同的文件夹中 move