我想通過觸摸來滾動我的地圖。你能告訴我如何達到它的基本概念嗎?這是我醜陋的簡單代碼。只需通過每次觸摸移動即可修改圖層的位置。它的工作原理,但它可能並不可愛。如何順利滾動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,謝謝,聽起來很有趣。我想嘗試自己實現這一點。練習。 –