5

当点击屏幕上的任何一点时,我有 1 把枪,子弹会发射出去,但根据我的要求,当触摸任何精灵时,有 3 把枪(精灵)子弹必须发射,当我用谷歌搜索时,我才知道这可以是通过使用targetedTouchDelegate或循环所有精灵并为每个触摸的精灵设置标志来完成,我已经在iphone中看到了这个代码,但找不到android,请任何人都可以告诉如何在android中使用它?或 cocos2d-android 的任何链接或书籍不仅对其他人有用。谢谢,

4

1 回答 1

0

Well what I would do in such a case would be get the rect for my sprite using this

CGRect projectileRect = CGRect
                .make(sprite.getPosition().x
                        - (sprite.getContentSize().width / 2.0f),
                        sprite.getPosition().y
                                - (sprite.getContentSize().height / 2.0f),
                        sprite.getContentSize().width,
                        sprite.getContentSize().height);

and I will detect if the clicked point is in the rectangle of that particular sprite you can override onccTouchBegan to get the clicked point and then look for the collision

@Override
public boolean ccTouchesBegan(MotionEvent event) {
    // TODO Auto-generated method stub

    CGPoint touchLocation=CGPoint.ccp(event.getX(), event.getY());
    CGRect targetRect = CGRect.make(
            event.getX(),
            event.getY(),
            5,
            5);

        if (CGRect.intersects(projectileRect, targetRect))
                  1st sprite is clicked 

    return super.ccTouchesBegan(event);

}

This is my work around.

于 2013-05-20T11:40:28.377 回答