0

我正在研究 cocos2d,我需要为 sprite 添加一个侦听器。我的代码在这里

public class GameOverScreen extends CCLayer implements KeyHandler {

public static CCScene scene(Difficulty difficulty, long totalscore) {

    CCScene result = CCScene.node();

    CCSprite redeem = CCSprite.sprite("redeem.png");// listener to this sprite
    CGSize s = CCDirector.sharedDirector().winSize();
    redeem.setScale(Block.SCALE*Main.SCALE);
    redeem.setPosition(-1, -1);

    redeem.setAnchorPoint(CGPoint.ccp(-0.7f, -1));

    result.addChild(new GameOverScreen(NewHighscore, totalscore));
    result.addChild(redeem);

    return result;


}
4

3 回答 3

0

你可以做这件事:

CCMenuItemImage item1 = CCMenuItemImage.item("redeem.png","redeem.png", this,"function_name");

CCMenu menu = CCMenu.menu(item1);
     menu.setPosition(CGPoint.make(0, 0));
     item1.setPosition(CGPoint.make(winSize.width/2, winSize.height/2));

当您点击兑换图像(Sprite)时使用 function_name

public void function_name(Object sender) 
{
// what you want to perform on the click like : enter the CCScene etc.  
}
于 2013-07-13T11:35:25.690 回答
0

我找到了解决方案,代码是这样的:

public static CCScene scene(Difficulty difficulty, long totalscore) {
    boolean NewHighscore = Settings.Current().addToHighscore(difficulty, totalscore);

    CCScene result = CCScene.node();

    CCSprite redeem = CCSprite.sprite("redeem.png");
    s = CCDirector.sharedDirector().winSize();
    redeem.setScale(Block.SCALE*Main.SCALE);
    redeem.setPosition(s.width/2, s.height/4);




    result.addChild(new GameOverScreen(NewHighscore, totalscore));
    result.addChild(redeem);


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



    return result;

}

@Override
public  boolean ccTouchesBegan(MotionEvent event) {


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




        if (CGRect.intersects(projectileRect, targetRect))
        {
            System.out.println("here inside intersection");
            // RevMob functionality
        }



    return super.ccTouchesBegan(event);


}
于 2013-07-13T12:25:43.040 回答
0
@Override
public boolean ccTouchesBegan(MotionEvent event) {
 location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));

     if(CGRect.containsPoint((redeem.getBoundingBox()), location)){
      //Action
     }  
 return true;
}

解决此问题的简短技巧

于 2013-07-13T13:01:28.733 回答