首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ccTouchesBegan在cocos2d-android中不起作用

ccTouchesBegan在cocos2d-android中不起作用
EN

Stack Overflow用户
提问于 2013-06-28 15:03:01
回答 2查看 456关注 0票数 3

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

我的玩家是

代码语言:javascript
复制
 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中没有收到任何错误和日志记录

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-19 02:17:02

你的代码很好,只需要用System.out.println()函数代替System.out.printf(), System.err.print()输出文本到控制台即可。当我替换它们时,文本已经出现在控制台中。

这一点:

代码语言:javascript
复制
System.out.print(":touch Points are - :"+projectileRect+" _  -  _ "+touchLocation+" _  -  _ "+targetRect);

更改为:

代码语言:javascript
复制
System.out.println(":touch Points are - :"+projectileRect+" _  -  _ "+touchLocation+" _  -  _ "+targetRect);

还有这个:

代码语言:javascript
复制
System.err.print(": This is intersect function from App :"); 

更改为:

代码语言:javascript
复制
System.out.println(": This is intersect function from App :");

还有这个:

代码语言:javascript
复制
System.out.printf("Got touch ", location);

更改为:

代码语言:javascript
复制
System.out.println("Got touch " + location);
票数 0
EN

Stack Overflow用户

发布于 2014-10-16 16:22:09

您需要在构造函数中添加以下内容:

代码语言:javascript
复制
this.setIsTouchEnabled(true);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17359063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档