当我在cocos2d android中触摸它时如何删除精灵?

时间:2013-02-18 13:11:58

标签: android cocos2d-android

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

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 个答案:

答案 0 :(得分: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);
}
}

答案 1 :(得分: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);
}

}

答案 2 :(得分: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用于此目的。

答案 3 :(得分:0)

单击时使用此选项删除图像:

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

//Remove the image when it intersects