cocos2d android 触摸精灵

发布于 2024-12-15 15:54:19 字数 1040 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫思念不要吵 2024-12-22 15:54:19

对此有一个最好的解决方案。使用:

sprite.getBoundingBox.contains(x,y);

其中x和y是触摸位置的位置。

There is a very best solution for that. Use:

sprite.getBoundingBox.contains(x,y);

where x and y are positions of touched location.

若有似无的小暗淡 2024-12-22 15:54:19

我希望这会对您有所帮助。我正在使用这种方式来处理特定恶意的触摸事件。

public boolean ccTouchesEnded(MotionEvent event) {
        CGPoint location = CCDirector.sharedDirector().convertToGL(
                CGPoint.ccp(event.getX(), event.getY()));
        if (CGRect.containsPoint((newGame1.getBoundingBox()), location)) {

            newGame();

        }

        return super.ccTouchesEnded(event);
    }

请将此添加到构造函数

this.setIsTouchEnabled(true);

I hope this will help you. I am using this way to handle touch event for particular spite.

public boolean ccTouchesEnded(MotionEvent event) {
        CGPoint location = CCDirector.sharedDirector().convertToGL(
                CGPoint.ccp(event.getX(), event.getY()));
        if (CGRect.containsPoint((newGame1.getBoundingBox()), location)) {

            newGame();

        }

        return super.ccTouchesEnded(event);
    }

please add this to constructor

this.setIsTouchEnabled(true);
回眸一遍 2024-12-22 15:54:19

虽然我不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文