且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获得精灵到触摸反应在cocos2d的Andr​​oid?

更新时间:2023-11-19 15:50:10

那么我会在这种情况下做的是使用得到矩形为我的这个精灵

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);

如果单击点是在特定精灵的矩形我将检测
您可以覆盖onccTouchBegan拿到单击点,然后寻找碰撞

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);

}

这是我的工作,各地。