我想通过触摸来滚动我的地图。你能告诉我如何达到它的基本概念吗?这是我丑陋的简单代码。只需通过每次触摸移动即可修改图层的位置。它的工作原理,但它可能并不可爱。如何顺利滚动CCLayer?
.h
CGPoint *touchBegin;
CGPoint *touchmove;
.m
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector]
convertToGL:touchLocation];
touchBegin=new CGPoint(touchLocation);
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation =
[[CCDirector sharedDirector] convertToGL:touchLocation];
touchmove = new CGPoint(touchLocation);
[self setPosition:ccp(self.position.x/100-(touchBegin->x-touchmove->x),self.position.y/100.0f)];
delete touchmove;
touchmove=NULL;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
if (touchBegin)
{
delete touchBegin;
touchBegin=NULL;
}
if (touchmove)
{
delete touchmove;
touchmove=NULL;
}
}
可爱的意思是什么?请记住,将图层移动到触摸位置时,如果不在触摸之间进行插值(触摸位置可能差异很大)并逐渐向最后一个触摸位置移动(通过使用CCMoveTo更新每帧的位置或作为拐杖),将永远不会获得平滑的结果。 – LearnCocos2D
@ LearnCocos2D,谢谢,听起来很有趣。我想尝试自己实现这一点。练习。 –