0

我试过这个解决方案....

public void update(float dt)
{ 
    CGRect targetRect = CGRect.make(target.getPosition().x -(target.getContentSize().width),target.getPosition().y - (target.getContentSize().height),target.getContentSize().width,target.getContentSize().height);
    if (CGRect.containsPoint(targetRect, location))
    {
       spriteMoveFinished(target);
    }
}

但我无法得到结果。

4

4 回答 4

2

我已检查您的代码您尚未安排方法更新。我已将更新方法编码移动到触摸方法,因此我们现在不需要更新来安排。下面是更新后的代码,它在我的最后工作正常.. 触摸工作正常

import java.util.ArrayList;

import org.cocos2d.actions.instant.CCCallFuncN;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCColorLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccColor4B;

import android.view.MotionEvent;

public class as extends CCColorLayer{
protected CCSprite target;
protected ArrayList<CCSprite> _targets;
CGPoint location;

public static CCScene scene(){
    CCScene scene = CCScene.node();
    CCColorLayer layer = new as(ccColor4B.ccc4(255, 255, 255, 255));
    scene.addChild(layer);
    return scene;
}
protected as(ccColor4B color){
    super(color);
    this.setIsTouchEnabled(true);
    _targets = new ArrayList<CCSprite>();
    this.schedule("gameLogic", 3.0f);
}       
@Override
public boolean ccTouchesBegan(MotionEvent event) {
     location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));

     ArrayList<CCSprite> targetsToDelete = new ArrayList<CCSprite>();
     for (CCSprite target : _targets){
         if(CGRect.containsPoint((target.getBoundingBox()), location))
                      targetsToDelete.add(target);
     }
     for (CCSprite target : targetsToDelete){
         _targets.remove(target);
         removeChild(target, true);
     }

     return true;
}


//public void update(float dt){
//
//}

public void gameLogic(float dt)
{
    addTarget();
}

protected void addTarget()
{
    CGSize winSize = CCDirector.sharedDirector().displaySize();
    target = CCSprite.sprite("bee_160.png");
    target.setPosition(CGPoint.ccp(target.getContentSize().width / 2.0f, winSize.height / 2.0f));
    addChild(target);    
    _targets.add(target);

    CCMoveTo actionMove = CCMoveTo.action(3, CGPoint.ccp(winSize.getWidth(), winSize.getHeight()/2.0f));
    CCCallFuncN actionMoveDone = CCCallFuncN.action(this, "spriteMoveFinished");
    CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);

    target.runAction(actions);
}

public void spriteMoveFinished(Object sender)
{
    CCSprite sprite = (CCSprite)sender;
    this.removeChild(sprite, true);
}
}
于 2013-02-26T07:30:48.577 回答
1
public class as extends CCColorLayer{
protected CCSprite target;
protected ArrayList<CCSprite> _targets;
CGPoint location;

public static CCScene scene(){
    CCScene scene = CCScene.node();
    CCColorLayer layer = new as(ccColor4B.ccc4(255, 255, 255, 255));
    scene.addChild(layer);
    return scene;
}
protected as(ccColor4B color){
    super(color);
    this.setIsTouchEnabled(true);
    _targets = new ArrayList<CCSprite>();
    this.schedule("gameLogic", 3.0f);
            this.schedule("update");
}       
@Override
public boolean ccTouchesBegan(MotionEvent event) {
     location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
     return true;
}

public void update(float dt){
    ArrayList<CCSprite> targetsToDelete = new ArrayList<CCSprite>();
    for (CCSprite target : _targets){
        if(CGRect.containsPoint((target.getBoundingBox()), location))
                     targetsToDelete.add(target);
    }
    for (CCSprite target : targetsToDelete){
        _targets.remove(target);
        removeChild(target, true);
    }
}

public void gameLogic(float dt)
{
    addTarget();
}

protected void addTarget()
{
    CGSize winSize = CCDirector.sharedDirector().displaySize();
    target = CCSprite.sprite("RedX.gif");
    target.setPosition(CGPoint.ccp(target.getContentSize().width / 2.0f, winSize.height / 2.0f));
    addChild(target);    
    _targets.add(target);

    CCMoveTo actionMove = CCMoveTo.action(3, CGPoint.ccp(winSize.getWidth(), winSize.getHeight()/2.0f));
    CCCallFuncN actionMoveDone = CCCallFuncN.action(this, "spriteMoveFinished");
    CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);

    target.runAction(actions);
}

public void spriteMoveFinished(Object sender)
{
    CCSprite sprite = (CCSprite)sender;
    this.removeChild(sprite, true);
}

}

于 2013-02-21T12:36:00.817 回答
0

单击时使用它来删除图像:

//rect1 and rect2 are two rectangles of two different images
CCRect.intersect(rect1,rect2))

//Remove the image when it intersects
于 2014-06-14T06:53:23.617 回答
0

您可以通过调用默认的 cocos 方法来检查精灵的触摸

首先启用触摸(Cocos2D Touch HELP

isTouchEnabled_=true;

之后在 ccToucesBegan 方法中,您可以检查触摸哪个精灵

(getBoundingBox() 返回 CGRect)

      @Override
      public boolean ccTouchesBegan(MotionEvent event) {

 CGPoint convertedLocation = CCDirector.sharedDirector()
                    .convertToGL(CGPoint.make(event.getX(), event.getY()));

if(sprite.getBoundingBox().contains(convertedLocation.x,convertedLocation.y))
{
removeChild(sprite, true);
}
}

在您的程序中,您没有指定精灵图像,这里是触摸正常工作的示例图层类

public class TestLayer extends CCColorLayer{ 

    protected CCSprite target; protected CGPoint location;

    public static CCScene scene(){
        CCScene scene = CCScene.node();
        CCColorLayer layer = new TestLayer(ccColor4B.ccc4(0, 0, 255, 255));

        scene.addChild(layer);

        return scene;
    }

    protected TestLayer(ccColor4B color){
        super(color);
        this.setIsTouchEnabled(true);

        target=CCSprite.sprite("bee_120.png");

        addChild(target);
        target.setPosition(100, 100);

//      this.schedule("game", 3.0f);


    }       
    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
    location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
        if(CGRect.containsPoint(target.getBoundingBox(), location));
            {
                removeChild(target, true);
            }
        return true;
    }
//  public void game(float dt){
//      addTarget();
//  }
//  protected void addTarget(){
//      //some code of add the "target"
//  }
}

在上面的代码中,只使用了一个精灵。要使用多个精灵,您必须像您所做的那样安排方法,但您必须检查您生成的所有精灵的触摸。您可以为此目的使用 ArrayList。

于 2013-02-21T11:15:12.263 回答