Drawing line on touches moved in COCOS2D(在 COCOS2D 中移动的触摸上画线)
问题描述
我正在使用 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 中移动的触摸上画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 COCOS2D 中移动的触摸上画线
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01