问题描述
我正在使用 COCOS2D 为 iPhone 开发游戏.
I'm developing a game for iPhone using COCOS2D.
其中,当用户将手指从一个点拖动到另一个点时,我需要画一条线.据我所知,我需要在 Touches Moved method
中执行此操作,我可以从中获得积分.
In that, I need to draw a line when user drag his finger from a point to another. As far as my knowledge is concern I need to do this in Touches Moved method
from where I can get the points.
但我不知道该怎么做.有人可以帮我解决这个问题吗?
But I don't know how to do this. Can anybody help me on this?
推荐答案
起亚ora.无聊迫使我就这个话题提供答案.
Kia ora. Boredom compels me to provide an answer on this topic.
图层部分(即@interface GetMyTouches : CCLayer):
Layer part (i.e. @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):
Node part (i.e. @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):
Layer part II (i.e. @interface GetMyTouches : CCLayer):
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
DrawMyTouch *line = [DrawMyTouch node];
[self addChild: line];
}
记住触摸很容易.触摸时知道自己在做什么不是火箭科学.
Remember touching is easy. Knowing what you're doing while touching isn't rocket science.
最后,如果你不明白我发布的任何内容......请选择烘焙.世界需要更多的巧克力蛋糕生产商.
Finally, if you don't understand anything i've posted ... take up baking. The world needs more chocolate cake producers.
澄清:
- 没有人能从 cut 'n paste 中学到东西~这个 代码 从来没有打算在没有爱抚的情况下工作
- 如果你看不到幽默,那你就错了职业
- No one learns from cut 'n paste ~ this code was never meant to work without caressing
- If you fail to see the humour, you're in the wrong profession
值得注意的是,我喜欢美味的巧克力蛋糕.世界确实需要更多出色的面包师.这不是侮辱,是鼓励.
Of note, I love a good chocolate cake. The world really does need more fantastic bakers. That's not an insult, that's encouragement.
看看广场外面,找到一个充满知识的圆圈,让生活变得有价值"~Aenesidemus.
这篇关于在 COCOS2D 中移动的触摸上画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!