0

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.

Akarsh M
  • 1,629
  • 2
  • 24
  • 47

4 Answers4

2

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);
}
}
Karan Rana
  • 638
  • 4
  • 14
  • I have schedule the update method in above code but when removing of first sprite the other not coming .. there is any problem in the code ??? – Akarsh M Feb 26 '13 at 07:57
  • This may be cause of scheduling of update method. You have to reset the location variable.. The better is to use code in on touch.. schedule method called at every interval if you have touched the screen or not... – Karan Rana Feb 26 '13 at 08:03
  • ok , i got the point ...thanks for help.. really it helps me sort out this problem ? – Akarsh M Feb 26 '13 at 08:29
  • I added some other problems ... If have any idea for them ...please share your exp. .. thanks in advance – Akarsh M Feb 26 '13 at 08:30
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);
}

}

Akarsh M
  • 1,629
  • 2
  • 24
  • 47
0

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.

Community
  • 1
  • 1
Karan Rana
  • 638
  • 4
  • 14
  • It's not working, error occur when I click on the anywhere on the screen . – Akarsh M Feb 21 '13 at 12:08
  • Have you initialize sprite?.. I have updated ans and added the example of working program – Karan Rana Feb 21 '13 at 13:19
  • Yes, I have done the initialization .... I have post the complete code at below post . please have a look on it ... – Akarsh M Feb 22 '13 at 08:33
  • this is work .... thanks for this ... but I want to remove the moving sprite .... its not applicable for it ... there is some another way to sort out the problem or this one is ok ?? – Akarsh M Feb 22 '13 at 11:38
  • You can also remove the moving sprite.. I have used the same way.. i have generated multiple sprite(moving forward) and added all sprite in ArrayList and checked on touch on every sprite in ArrayList. If the touch on any one sprite true then I have removed the sprite – Karan Rana Feb 23 '13 at 11:58
  • I am using the same way but when i clicked on moving sprite,Termination occurred...!!! i use the debugger for the checking but couldnot get any solution. – Akarsh M Feb 23 '13 at 13:20
  • ArrayList targetsToDelete = new ArrayList(); for (CCSprite target : _targets) { if(CGRect.containsPoint((target.getBoundingBox()), location)) targetsToDelete.add(target); } for (CCSprite target : targetsToDelete) { _targets.remove(target); removeChild(target, true); } – Akarsh M Feb 23 '13 at 13:39
  • Can you post complete code, so I can check what the error in code. – Karan Rana Feb 25 '13 at 12:48
0

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
Jamal
  • 763
  • 7
  • 22
  • 32