2012-10-12 51 views
0

我遇到的問題與Android的cocos2d。我嘗試了多種方法,但似乎無法讓精靈在屏幕上移動。兩張照片都顯示出來,但電腦精靈不動。運行代碼時不會出現任何錯誤,但除了兩個沒有移動的圖片外,沒有任何反應。精靈不會在cocos2d中移動對於android

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

回答

0

一個小小的錯誤,即,在側面moveComputer()你沒有傳遞argumets.pass參數,像下面那麼你的對象將移動

public void moveComputer(float dt) 
{ 
    // 
} 
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())); 
檢查的區別