cocos2d-android - ccTouchesBegan 在 cocos2d-android 中不起作用

标签 cocos2d-android

我启用了setIsTouchEnabled(true);,但我无法在cocos2d-android中检测到触摸开始和触摸结束。

我的玩家是

 public class GameLayer extends CCColorLayer
 {
   protected CCLabel _label;

   public static CCScene scene()
   {
    CCScene scene = CCScene.node();
    GameLayer layer = new GameLayer(ccColor4B.ccc4(90, 90, 255, 255));
    layer.getLabel();

    scene.addChild(layer);
    return scene;

}

public CCLabel getLabel()
{
    return _label;

}

protected GameLayer(ccColor4B color)
{
    super(color);
    this.setIsTouchEnabled(true);
    CGSize winSize = CCDirector.sharedDirector().displaySize();
    Context context = CCDirector.sharedDirector().getActivity();
    _label = CCLabel.makeLabel("Tap On Me to START the game", "DroidSans", 32);
    _label.setColor(ccColor3B.ccBLACK);
    _label.setPosition(winSize.width/2, winSize.height/2);

    addChild(_label);

}

@Override
public boolean ccTouchesBegan(MotionEvent event)
{

    CGRect projectileRect = CGRect.make(_label.getPosition().x,_label.getPosition().y, _label.getContentSize().width,_label.getContentSize().height);
    CGPoint touchLocation = CGPoint.ccp(event.getX(), event.getY());
    CGRect targetRect = CGRect.make(event.getX(), event.getY(), 20, 20);
    System.out.print(":touch Points are - :"+projectileRect+" _  -  _ "+touchLocation+" _  -  _ "+targetRect);
    if (CGRect.intersects(projectileRect, targetRect))
    {
        System.err.print(": This is intersect function from App :");
    }
    return true;

}

@Override
public boolean ccTouchesEnded(MotionEvent event)
{
    // Choose one of the touches to work with
    CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(50, 200));
    System.out.printf("Got touch ", location);
    CGSize winSize = CCDirector.sharedDirector().displaySize();

    return true;

}
 }

有人能指出我哪里出了问题吗?我没有收到任何错误,并且 logcat 中没有跟踪任何日志

最佳答案

你的代码很好,只需使用System.out.println()函数将文本输出到控制台,而不是System.out.printf()、System.err.print() 。当我替换它们时,文本出现在控制台中。

这个:

System.out.print(":touch Points are - :"+projectileRect+" _  -  _ "+touchLocation+" _  -  _ "+targetRect);

更改为:

System.out.println(":touch Points are - :"+projectileRect+" _  -  _ "+touchLocation+" _  -  _ "+targetRect);

还有这个:

System.err.print(": This is intersect function from App :"); 

更改为:

System.out.println(": This is intersect function from App :");

还有这个:

System.out.printf("Got touch ", location);

更改为:

System.out.println("Got touch " + location);

关于cocos2d-android - ccTouchesBegan 在 cocos2d-android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17359063/

相关文章:

Android 图形内存限制

android - 如何在扩展 CCLayer 的类中使用 getAssets

安卓NDK : WARNING: Ignoring unknown import directory: C

android - Cocos2d android 中的动画 Sprite

java - 如何设置CCSprite的框架

android - 开始使用 cocos2d for android

compilation - cocos2dx3.10 android 编译错误

android - 如何让 Sprite 对cocos2d android中的触摸使用react?

c++ - eclipse 可以使用像 cocos2d 这样的外部库来自动完成吗?