Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何删除与cocos2d android中的另一个精灵相交时的精灵_Java_Ccsprite_Cocos2d Android - Fatal编程技术网

Java 如何删除与cocos2d android中的另一个精灵相交时的精灵

Java 如何删除与cocos2d android中的另一个精灵相交时的精灵,java,ccsprite,cocos2d-android,Java,Ccsprite,Cocos2d Android,在我的cocos2d android游戏应用程序中,有一个投射物、目标和一艘船,如果投射物和目标相交,两者都将消失,这工作正常,但当目标落在船上(相交)船没有被移除时,我尝试在谷歌搜索,但无法解决问题,如果有人知道这一点,请帮助我。 这是它的代码 public class GameL extends CCLayer{ protected LinkedList<CCSprite> _ships; protected LinkedList<CCSprite> _targets

在我的cocos2d android游戏应用程序中,有一个投射物、目标和一艘船,如果投射物和目标相交,两者都将消失,这工作正常,但当目标落在船上(相交)船没有被移除时,我尝试在谷歌搜索,但无法解决问题,如果有人知道这一点,请帮助我。 这是它的代码

public class GameL extends CCLayer{
protected LinkedList<CCSprite> _ships;
protected LinkedList<CCSprite> _targets;
protected LinkedList<CCSprite> _projectiles;
//protected int _projectilesDestroyed;
protected int _shipDestroyed;
protected CCSprite _player;
protected CCSprite _nextProjectile;
protected CCSprite ship;

public static CCScene scene()
{
    CCScene scene = CCScene.node();
    CCLayer layer = new GameL();
    scene.addChild(layer);  
    return scene;     
}


protected GameL()
{
  this.setIsTouchEnabled(true);
  _ships = new LinkedList<CCSprite>();
    _targets = new LinkedList<CCSprite>();
    _projectiles = new LinkedList<CCSprite>();
    //_projectilesDestroyed = 0;
    _shipDestroyed = 0;

    CCSprite background = CCSprite.sprite("bg.png");
    background.setTag(1);
    background.setAnchorPoint(0, 0);
    addChild(background);

    Context context = CCDirector.sharedDirector().getActivity();

    CGSize winSize = CCDirector.sharedDirector().displaySize();
 _player = CCSprite.sprite("gun2.png");
_player.setPosition(CGPoint.ccp(65,120));
 // _player.setPosition(CGPoint.ccp(_player.getContentSize().width/2.0f, winSize.height/2.0f));
     addChild(_player);

    CCSprite ship = CCSprite.sprite("ship150.png");
     ship.setPosition(CGPoint.ccp(25,100));
     ship.setAnchorPoint(CGPoint.ccp(0,0));
     ship.setTag(4);
        addChild(ship);

    this.schedule("gameLogic", 1.0f);
    this.schedule("update");
   }


@Override
public boolean ccTouchesEnded(MotionEvent event)
{
    // Choose one of the touches to work with
    CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));

    // Set up initial location of projectile
    CGSize winSize = CCDirector.sharedDirector().displaySize();
CCSprite _nextProjectile = CCSprite.sprite("firebl.png");  

    //_nextProjectile.setPosition(20, winSize.height / 2.0f);
_nextProjectile.setPosition(CGPoint.ccp(65, 120));

    // Determine offset of location to projectile
    int offX = (int)(location.x - _nextProjectile.getPosition().x);
    int offY = (int)(location.y - _nextProjectile.getPosition().y);

    // Bail out if we are shooting down or backwards
    if (offX <= 0)
        return true;

    _nextProjectile.setTag(2);

    // Determine where we wish to shoot the projectile to
    int realX = (int)(winSize.width + (_nextProjectile.getContentSize().width / 2.0f));
    float ratio = (float)offY / (float)offX;
    int realY = (int)((realX * ratio) + _nextProjectile.getPosition().y);
    CGPoint realDest = CGPoint.ccp(realX, realY);

    // Determine the length of how far we're shooting
    int offRealX = (int)(realX - _nextProjectile.getPosition().x);
    int offRealY = (int)(realY - _nextProjectile.getPosition().y);
    float length = FloatMath.sqrt((offRealX * offRealX) + (offRealY * offRealY));
    float velocity = 480.0f / 1.0f; // 480 pixels / 1 sec
    float realMoveDuration = length / velocity;

    // Move projectile to actual endpoint
    _nextProjectile.runAction(CCSequence.actions(
            CCMoveTo.action(realMoveDuration, realDest),
            CCCallFuncN.action(this, "spriteMoveFinished")));

    // Determine angle to face
    double angleRadians = Math.atan((double)offRealY / (double)offRealX);
    double angleDegrees = Math.toDegrees(angleRadians);
    double cocosAngle = -1 * angleDegrees;
    double rotationSpeed = 0.5 / Math.PI;
    double rotationDuration = Math.abs(angleRadians * rotationSpeed);
    _player.runAction(CCSequence.actions(
            CCRotateTo.action((float)rotationDuration, (float)cocosAngle),
            CCCallFunc.action(this, "finishShoot")));

    // Pew!
    Context context = CCDirector.sharedDirector().getActivity();
    SoundEngine.sharedEngine().playEffect(context, R.raw.pew_pew_lei);

    return true;
}  

public void finishShoot()
{
    addChild(_nextProjectile);
    _projectiles.add(_nextProjectile);
}

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

public void update(float dt)
{
    LinkedList<CCSprite> projectilesToDelete = new LinkedList<CCSprite>();

    for (CCSprite projectile : _projectiles)
    {
        CGRect projectileRect = CGRect.make(projectile.getPosition().x - (projectile.getContentSize().width / 2.0f),
                                            projectile.getPosition().y - (projectile.getContentSize().height / 2.0f),
                                            projectile.getContentSize().width,
                                            projectile.getContentSize().height);




        LinkedList<CCSprite> targetsToDelete = new LinkedList<CCSprite>();

        for (CCSprite target : _targets)
        {
            CGRect targetRect = CGRect.make(target.getPosition().x - (target.getContentSize().width),
                                            target.getPosition().y - (target.getContentSize().height),
                                            target.getContentSize().width,
                                            target.getContentSize().height);

            if (CGRect.intersects(projectileRect, targetRect))
                targetsToDelete.add(target);
        }

        LinkedList<CCSprite> shipsToDelete = new LinkedList<CCSprite>();
        for (CCSprite ship : _ships)
         {
                CGRect shipRect = CGRect.make(ship.getPosition().x - (ship.getContentSize().width),
                                             ship.getPosition().y - (ship.getContentSize().height),
                                             ship.getContentSize().width,ship.getContentSize().height);


                for (CCSprite target : _targets)
                  {
                        CGRect targetRect = CGRect.make(target.getPosition().x - (target.getContentSize().width),
                                                                                  target.getPosition().y -
                                                                                (target.getContentSize().height),
                                                                target.getContentSize().width,target.getContentSize().height);


                     if (CGRect.intersects(targetRect, shipRect))
                        {
                                shipsToDelete.add(ship);
                                break;
                        }
                }                      
         }

        for (CCSprite target : targetsToDelete)
        {
            _targets.remove(target);
            removeChild(target, true);
        }

        if (targetsToDelete.size() > 0)
            projectilesToDelete.add(projectile);

        for (CCSprite ship : shipsToDelete)
        {
            _ships.remove(ship);
            removeChild(ship, true);
        }
    }

    for (CCSprite projectile : projectilesToDelete)
    {
        _projectiles.remove(projectile);
        removeChild(projectile, true);

        if (_shipDestroyed > 0)
        {
            _shipDestroyed = 0;
            CCDirector.sharedDirector().replaceScene(Gameoverlayer.scene("You Win!"));
        }
    }

}

protected void addTarget()
{
    Random rand = new Random();
    CCSprite target = CCSprite.sprite("fireball.png");

    // Determine where to spawn the target along the Y axis
    CGSize winSize = CCDirector.sharedDirector().displaySize();
    int minX = (int)(target.getContentSize().width / 2.0f);
    int maxX = (int)(winSize.width - target.getContentSize().width / 2.0f);
    int rangeX = maxX - minX;
    int actualX = rand.nextInt(rangeX) + minX;
    // Create the target slightly off-screen along the right edge,
    // and along a random position along the Y axis as calculated above
//  target.setPosition(getContentSize().width + (target.getContentSize().width / 2.0f), actualX);
    target.setPosition(actualX, winSize.height  +  target.getContentSize().height);
    addChild(target);
    target.setTag(1);
    _targets.add(target);

    // Determine speed of the target
    int minDuration = 2;
    int maxDuration = 4;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = rand.nextInt(rangeDuration) + minDuration;

    // Create the actions
    //CCMoveTo actionMove = CCMoveTo.action(actualDuration, CGPoint.ccp(-target.getContentSize().width / 2.0f, actualX));
    CCMoveTo actionMove = CCMoveTo.action(actualDuration, CGPoint.ccp(actualX,  - target.getContentSize().height));
    CCCallFuncN actionMoveDone = CCCallFuncN.action(this, "spriteMoveFinished");
    CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);

    target.runAction(actions);
}

public void spriteMoveFinished(Object sender)
{
    CCSprite sprite = (CCSprite)sender;

    if (sprite.getTag() == 1)
    {
        _targets.remove(sprite);  

        _shipDestroyed = 0;
        //CCDirector.sharedDirector().replaceScene(Gameoverlayer.scene("You Lose :("));
    }
    else if (sprite.getTag() == 2)
        _projectiles.remove(sprite);

    this.removeChild(sprite, true);
}
   }
public class GameL扩展了CCLayer{
受保护的LinkedList船舶;
受保护的LinkedList_目标;
受保护的LinkedList投射物;
//受保护的int_投射物被销毁;
受保护的国际船舶被摧毁;
受保护的CCSprite\u播放器;
受保护的CCSprite下一个项目;
受保护的雪碧船;
公共静态场景
{
CCScene=CCScene.node();
CCLayer layer=新游戏();
scene.addChild(层);
返回场景;
}
受保护的GameL()
{
此.setIsTouchEnabled(true);
_ships=newlinkedlist();
_targets=新的LinkedList();
_投射=新的LinkedList();
//_射影数=0;
_平均值=0;
CCSprite background=CCSprite.sprite(“bg.png”);
背景:setTag(1);
背景:setAnchorPoint(0,0);
addChild(背景);
Context Context=CCDirector.sharedDirector().getActivity();
CGSize winSize=CCDirector.sharedDirector().displaySize();
_player=CCSprite.sprite(“gun2.png”);
_玩家设置位置(CGPoint.ccp(65120));
//_player.setPosition(CGPoint.ccp(_player.getContentSize().width/2.0f,winSize.height/2.0f));
addChild(_player);
CCSprite ship=CCSprite.sprite(“ship150.png”);
船舶设置位置(CGPoint.ccp(25100));
船舶设定点(CGPoint.ccp(0,0));
船舶设置标签(4);
addChild(船舶);
本附录(“游戏逻辑”,1.0f);
本附表(“更新”);
}
@凌驾
公共布尔值ccTouchesEnded(运动事件)
{
//选择要使用的触摸之一
CGPoint location=CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(),event.getY());
//设置投射物的初始位置
CGSize winSize=CCDirector.sharedDirector().displaySize();
CCSprite _nextproject=CCSprite.sprite(“firebl.png”);
//_nextproject.setPosition(20,winSize.height/2.0f);
_NEXTPROJECTLE.SETPOSION(CGPoint.ccp(65120));
//确定位置对射弹的偏移量
int offX=(int)(location.x-_nextproject.getPosition().x);
int offY=(int)(location.y-_nextproject.getPosition().y);
//如果我们正在向下射击或向后射击,请救出

如果(offX您确定用于检测碰撞的矩形正确吗?据我所知,精灵的默认定位点为{0.5;0.5},因此矩形计算更像:

CGRect shipRect = CGRect.make(ship.getPosition().x - (ship.getContentSize().width / 2),
                                             ship.getPosition().y - (ship.getContentSize().height / 2),
                                             ship.getContentSize().width,ship.getContentSize().height);