精灵不会在cocos2d中移动到android

时间:2012-10-12 00:28:21

标签: android cocos2d-iphone cocos2d-android

我遇到了针对Android的cocos2d问题。我尝试了多种方法,但似乎无法让精灵在屏幕上移动。两张图片都显示出来,但计算机精灵不会移动。运行代码时没有出现错误,但除了显示没有移动的两张图片之外没有任何反应。

    package com.example.minigameapp;

import java.util.Random;

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.CGSize;
import org.cocos2d.types.ccColor4B;


import android.util.Log;

public class GameLayer extends CCColorLayer{
    CCSprite _player;
    CCSprite _computer;
    public static CCScene scene()
    {
        CCScene scene = CCScene.node();
        CCColorLayer layer = new GameLayer(ccColor4B.ccc4(255, 255, 255, 255));

        scene.addChild(layer);

        return scene;
    }

    protected GameLayer(ccColor4B color)
    {
        super(color);
        this.setIsTouchEnabled(true);
        CGSize winSize = CCDirector.sharedDirector().displaySize();

        _player = CCSprite.sprite("race_car2.png");
        _computer = CCSprite.sprite("race_car.png");
        _player.setPosition(CGPoint.ccp(_player.getContentSize().width / 2.0f, winSize.height / 2.0f));
        _computer.setPosition(CGPoint.ccp(_computer.getContentSize().width / 2.0f,winSize.height / 2.0f + _player.getContentSize().height*3));


        addChild(_player);
        addChild(_computer);
        this.schedule("moveComputer",1.0f);
    }
    public void moveComputer(){
        CGSize winSize = CCDirector.sharedDirector().displaySize();
        float finalX=winSize.width;

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

        Log.d("GameLayer","Set Action");
        //CCMoveTo actionMove = CCMoveTo.action(actualDuration, CGPoint.ccp(-computer.getContentSize().width / 2.0f, finalX));
        //CCCallFuncN actionMoveDone = CCCallFuncN.action(this, "spriteMoveFinished");
        //CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);
        //computer.runAction(actions);
        CGPoint point = CGPoint.ccp(finalX,winSize.height / 2.0f + _player.getContentSize().height*3);
        CCMoveTo actionMove = CCMoveTo.action(10, point);
        _computer.runAction(actionMove);
        Log.d("GameLayer", "Start Moving");
    }

    public void spriteMoveFinished(Object sender)
    {
        Log.d("GameLayer", "Finished Moving");
        //CCSprite sprite = (CCSprite)sender;
        //this.removeChild(sprite, true);
        this.removeChild(_computer, true);
        Log.d("GameLayer", "Remove Sprite");
    }
}

2 个答案:

答案 0 :(得分:1)

您已修复CGPoint的位置

CGPoint point = CGPoint.ccp(finalX,winSize.height / 2.0f + _player.getContentSize().height*3);
        CCMoveTo actionMove = CCMoveTo.action(10, point);

您还必须在每个计划中更改CGPoint值。

您的预定方法正在运行,但每次调用方法时CG点值都相同,并且您已使用CCMoveTo将精灵移动到屏幕上的特定位置。所以精灵已经在你要移动的位置,以便显示静止(不移动)。

每次调度方法调用时更改CGPoint值...

您可以通过将CGPoint值替换为

来检查差异
CGPoint point = CGPoint.ccp(finalX+(100*Math.random()),winSize.height / 2.0f + player.getContentSize().height*3+(100*Math.random()));

答案 1 :(得分:0)

一个小错误,即在moveComputer()旁边没有传递argumets.pass个参数,如下所示,你的对象将移动

public void moveComputer(float dt)
{
  //
}