cocos2d android 触摸精灵
我是 cocos2d 的新手,我想知道如何在 java 中编写代码来检查我是否触摸过精灵,我已经尝试过类似的方法了。
@Override
public boolean ccTouchesEnded(MotionEvent event)
{
CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
if ((location.x == zom.getPosition().x) && (location.y == zom.getPosition().y))
{
CCSprite projectile = CCSprite.sprite("bullet.png");
projectile.setPosition(CGPoint.ccp(player.getPosition().x,player.getPosition().y));
addChild(projectile);
float length = (float)Math.sqrt((100 * 100) + (100 * 100));
float velocity = 100.0f / 1.0f;
float realMoveDuration = length / velocity;
projectile.runAction(CCSequence.actions(
CCMoveTo.action(realMoveDuration, CGPoint.ccp(location.x, location.y)),
CCCallFuncN.action(this, "spriteMoveFinished")));
if ((projectile.getPosition().x == location.x) && ( projectile.getPosition().y == location.y))
{
removeChild(projectile, true);
}
}
I'm new to cocos2d and I was wondering how do I write a code in java that checks to see if I've touched a sprite I've already tried something like this..
@Override
public boolean ccTouchesEnded(MotionEvent event)
{
CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
if ((location.x == zom.getPosition().x) && (location.y == zom.getPosition().y))
{
CCSprite projectile = CCSprite.sprite("bullet.png");
projectile.setPosition(CGPoint.ccp(player.getPosition().x,player.getPosition().y));
addChild(projectile);
float length = (float)Math.sqrt((100 * 100) + (100 * 100));
float velocity = 100.0f / 1.0f;
float realMoveDuration = length / velocity;
projectile.runAction(CCSequence.actions(
CCMoveTo.action(realMoveDuration, CGPoint.ccp(location.x, location.y)),
CCCallFuncN.action(this, "spriteMoveFinished")));
if ((projectile.getPosition().x == location.x) && ( projectile.getPosition().y == location.y))
{
removeChild(projectile, true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对此有一个最好的解决方案。使用:
其中x和y是触摸位置的位置。
There is a very best solution for that. Use:
where x and y are positions of touched location.
我希望这会对您有所帮助。我正在使用这种方式来处理特定恶意的触摸事件。
请将此添加到构造函数
I hope this will help you. I am using this way to handle touch event for particular spite.
please add this to constructor
虽然我不是 cocos2d 高手,但检查代码时逻辑似乎有点不对劲。您想要检查触摸点是否位于精灵当前区域(即
((location.x >= sprite.start.x && location.x <= sprite.width) & ;& ((location.y >= sprite.start.y && location.y <= sprite.height)
。我认为更好的方法是扩展 sprite 类并包含一个函数检查并查看一个点是否在精灵区域中(
float isInSpriteArea(CGPoint point)
),这样您可以将一个点传递给精灵,它可以告诉您是否在这种情况下。感动。While I am not a cocos2d master, it looks like the logic is a bit off in checking your code. You want to check to see if the touch point is in the sprites current area (i.e. is
((location.x >= sprite.start.x && location.x <= sprite.width) && ((location.y >= sprite.start.y && location.y <= sprite.height)
.I think a better way is to extend the sprite class and include a function to check and see if a point is in the sprite area (
float isInSpriteArea(CGPoint point)
). That way you can just pass a point to a sprite and it can tell you if it is in this case touched.