如何使用弹弓在不同角度移动物体?
我是 iPhone 应用程序开发和 Xcode 的初学者,我正在使用 Box2D 开发一个具有弹弓效果的小游戏。当使用弹弓时,我需要帮助以各自的角度移动身体。弹弓是使用 ccDrawLine 绘制的,并在其上放置了一个主体。
在我的项目中,当使用弹弓时,物体会朝不同方向随意移动。有谁知道如何解决这个问题?
我的代码:
绘制弹弓:
-(void)draw
{
//NSLog(@"in dra");
//glDisable(GL_TEXTURE_2D);
//glDisableClientState(GL_COLOR_ARRAY);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
_world->DrawDebugData();
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glColor4f(0.6, 0.4, 0.2, 1.0);
glLineWidth(4.0f);
//glEnable(GL_LINE_SMOOTH);
ccDrawLine( ccp(80, 75),ccp(pt1,pt2));
ccDrawLine(ccp(pt1,pt2), ccp(240,75));
ccDrawLine(ccp(80,75),ccp(80,0));
ccDrawLine(ccp(240,75),ccp(240,0));
}
将物体放在吊索上:
-(void)addsprite2
{
stone=[CCSprite spriteWithFile:@"rock.png"];
stone.position=ccp(160,80);
stone.tag=1;
[self addChild:stone];
}
我正在尝试手动创建和添加角度。
角度创建:
if (stone.position.y < 80 && stone.position.y >= 70)
{
ft = abs(tp2)/PTM_RATIO;
}
else if(stone.position.y < 70 && stone.position.y >=60)
{
ft = (abs(tp2)+90)/PTM_RATIO;
}
else if(stone.position.y < 60 && stone.position.y >= 50)
{
ft = (abs(tp2)+110)/PTM_RATIO;
}
else if(stone.position.y < 50 && stone.position.y >= 40)
{
ft = (abs(tp2)+140)
I'm a beginner in iPhone Application Development and Xcode and I'm developing a small game using Box2D which has a slingshot effect. I need help with moving a body in its respective angles when the sling shot is used. The slingshot is drawn using ccDrawLine
and a body is placed on it.
In my project the object moves haphazardly in different directions when the sling shot is used. Does anyone know how to fix this?
My code:
Draw the slingshot:
-(void)draw
{
//NSLog(@"in dra");
//glDisable(GL_TEXTURE_2D);
//glDisableClientState(GL_COLOR_ARRAY);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
_world->DrawDebugData();
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glColor4f(0.6, 0.4, 0.2, 1.0);
glLineWidth(4.0f);
//glEnable(GL_LINE_SMOOTH);
ccDrawLine( ccp(80, 75),ccp(pt1,pt2));
ccDrawLine(ccp(pt1,pt2), ccp(240,75));
ccDrawLine(ccp(80,75),ccp(80,0));
ccDrawLine(ccp(240,75),ccp(240,0));
}
Place the object on the sling:
-(void)addsprite2
{
stone=[CCSprite spriteWithFile:@"rock.png"];
stone.position=ccp(160,80);
stone.tag=1;
[self addChild:stone];
}
I'm trying to create and add the angles manually.
Angle creation:
if (stone.position.y < 80 && stone.position.y >= 70)
{
ft = abs(tp2)/PTM_RATIO;
}
else if(stone.position.y < 70 && stone.position.y >=60)
{
ft = (abs(tp2)+90)/PTM_RATIO;
}
else if(stone.position.y < 60 && stone.position.y >= 50)
{
ft = (abs(tp2)+110)/PTM_RATIO;
}
else if(stone.position.y < 50 && stone.position.y >= 40)
{
ft = (abs(tp2)+140)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用贝塞尔曲线在2D空间中实现弹丸曲线效果。
然后根据拉动距离设置第一和第二控制点以及终点。
这是实现弹丸曲线的最简单方法。
另外,要移动对象,您可以使用touchesMoved函数并将对象的位置设置为与touchesMoved点的位置相同。
这将允许您在移动手指时移动对象。
You can use a bezier curve to implement the projectile curve effect in a 2D space.
Then set the the first and second control points and the end point depending on the pull distance.
This is the simplest way to implement the projectile curve.
Also to move the object you may use touchesMoved function and set the position of the object as the same as that of the touchesMoved point.
This will allow you to move the object as you move your finger.