我正在使用 COCOS2D 为 iPhone 开发游戏。
在那里,当用户将他的手指从一个点拖到另一个点时,我需要画一条线。据我所知,我需要在 Touches Moved 方法
中执行此操作,从那里我可以获得分数。
但是我不知道该怎么做。有人可以帮我解决这个问题吗?
最佳答案
起亚奥拉。无聊迫使我就此主题提供一个答案。
图层部分(即@interface GetMyTouches : CCLayer):
-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event
{
UITouch *touchMyMinge = [inappropriateTouches anyObject];
CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ];
CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]];
// flip belly up. no one likes being entered from behind.
currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];
// throw to console my inappropriate touches
NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y);
NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y);
// add my touches to the naughty touch array
naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)];
naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)];
}
节点部分(即@interface DrawMyTouch: CCNode):
@implementation DrawMyTouch
-(id) init
{
if( (self=[super init]))
{ }
return self;
}
-(void)draw
{
glEnable(GL_LINE_SMOOTH);
for(int i = 0; i < [naughtyTouchArray count]; i+=2)
{
start = CGPointFromString([naughtyTouchArray objectAtIndex:i]);
end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]);
ccDrawLine(start, end);
}
}
@end
图层第二部分(即@interface GetMyTouches : CCLayer):
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
DrawMyTouch *line = [DrawMyTouch node];
[self addChild: line];
}
记住触摸很容易。在触摸时知道自己在做什么并不是什么高深的科学。
最后,如果您不理解我发布的任何内容...开始烘焙吧。世界需要更多的巧克力蛋糕生产商。
澄清:
- 没有人能从剪切和粘贴中学到东西~这个代码从来就不是为了在没有爱抚的情况下工作
- 如果你看不到幽默,那你就入错行了
值得注意的是,我喜欢美味的巧克力蛋糕。世界确实需要更多出色的面包师。这不是侮辱,这是鼓励。
“看看正方形外面,找到充满知识的圆圈,让生活变得有值(value)”~ Aenesidemus。
关于ios - 在 COCOS2D 中移动的触摸画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4538598/