How to remove the sprite when I touch on it in cocos2d android?

独自空忆成欢 提交于 2019-12-08 13:00:09

问题


I tried this for the solution ....

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);
    }
}

but i can't get the result.


回答1:


I have checked your code You have not scheduled the method update. I have moved the update method coding to touch method so we don't need of update to schedule now. Below is the updated code which working fine at my end.. touch working properly

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);
}
}



回答2:


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);
}

}




回答3:


You can check the touch on sprite by calling default cocos methods

Firstly enable the touch(Cocos2D Touch HELP)

isTouchEnabled_=true;

after that in ccToucesBegan method you can check the touch on which sprite

(getBoundingBox() returns the 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);
}
}

As in your program you have not specified the sprite image here is the example layer class in which touch working properly

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"
//  }
}

In above code only one sprite used.. to use multiple sprite you have to schedule method as you have done but you have to check the touch for all sprite which you have generated. You can use ArrayList for that purpose.




回答4:


Use this to remove images when clicked on:

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

//Remove the image when it intersects


来源:https://stackoverflow.com/questions/14937185/how-to-remove-the-sprite-when-i-touch-on-it-in-cocos2d-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!