2014-02-24 100 views
1

我的代碼有問題,在地精XNA中設計坦克。我的問題是在增強現實中的地面標記上移動坦克。我得到了移動對象的代碼,但是我得到了一個我無法解決的錯誤。我試圖使用幾何節點,移動的代碼是使用對象。如何在Goblin XNA中移動對象?

以下是我的代碼。有人可以幫助我使用此代碼使用幾何節點嗎?

KeyboardState keyboardState = Keyboard.GetState(); 

if (keyboardState.IsKeyDown(Keys.Right)) 
{ 
    yourSprite.position.X += 1; 
} 
if (keyboardState.IsKeyDown(Keys.Left)) 
{ 
    yourSprite.position.X -= 1; 
} 
if (keyboardState.IsKeyDown(Keys.Up)) 
{ 
    yourSprite.position.Y -= 1; 
} 
if (keyboardState.IsKeyDown(Keys.Down)) 
{ 
    yourSprite.position.Y += 1; 
} 

A downloadable version of my program

+1

你得到什麼錯誤? – Joe

+1

我無法獲得聲明,因爲它的幾何節點錯誤信息是(名稱'yourspirit'在當前上下文中不存在。 – user3346249

回答

0

從本質上講,所有你需要的是一個類級別Vector2(位置),並繪製紋理。我相信yourSprite的課程包含這些內容。如果你不能得到它的工作,然後嘗試以下方法:

  1. 在類級別,定義了一個名爲Vector2position其設置爲你的初始位置(即屏幕中心)

  2. 在更新()方法,請使用您發佈的代碼,但更換

    yourSprite.position 
    

    position 
    

    這將更新我們想要角色位置的位置。

  3. 在您的Draw()方法中,再次將yourSprite.position替換爲position

+0

我如何向您發送我的完整代碼以幫助我 – user3346249

+0

如果可以,最好是在這裏發佈代碼,或者提供鏈接到您的遊戲基於您的示例。 – user3256944

+0

我將向您發送代碼和鏈接,我得到這個代碼的網站 – user3346249

1

首先要創建的對象似乎在此代碼,如果你創建對象請張貼錯誤信息

private void CreateObjects() 
    { 
     bowlingAlley = new Box(Vector3.One); 
     Material bowlingAlleyMaterial = new Material(); 
     bowlingAlleyMaterial.Specular = Color.Brown.ToVector4(); 
     bowlingAlleyMaterial.Diffuse = Color.BurlyWood.ToVector4(); 
     bowlingAlleyMaterial.SpecularPower = 45; 

     bowlingBall = new Sphere(3f, 50, 50); 
     bowlingBallMaterial = new Material(); 
     bowlingBallMaterial.Specular = Color.Black.ToVector4(); 
     bowlingBallMaterial.Diffuse = Color.BlanchedAlmond.ToVector4(); 

     alleyGroundMarker = new MarkerNode(scene.MarkerTracker, "AlvarGroundArray.xml"); 

     groundNode = new GeometryNode("Ground"); 
     groundNode.Model = bowlingAlley; 
     groundNode.Material = bowlingAlleyMaterial; 

     groundNode.Physics.MaterialName = "Ground"; 
     groundNode.Physics.Interactable = true; 
     groundNode.Physics.Collidable = true; 
     groundNode.Physics.Shape = GoblinXNA.Physics.ShapeType.Box;    
     groundNode.AddToPhysicsEngine = true; 


     // Create a parent transformation for both the ground and the sphere models 
     TransformNode transformBowlingAlley = new TransformNode(); 
     transformBowlingAlley.Translation = new Vector3(0,-10,-20); 

     // Create a scale transformation for the ground to make it bigger 
     TransformNode groundScaleNode = new TransformNode(); 
     groundScaleNode.Scale = new Vector3(400, 400, 10); 

     // Add this ground model to the scene 
     scene.RootNode.AddChild(alleyGroundMarker); 
     scene.RootNode.AddChild(transformBowlingAlley); 
     alleyGroundMarker.AddChild(groundScaleNode); 
     groundScaleNode.AddChild(groundNode); 

    } 
+0

groundNode,bowlingAlley,bowlingBallMaterial,bowlingBall和alleyGroundMarker的聲明是什麼。 – user3346249

+1

你可以用你的對象替換這個,任何方式看看這個[link](http://stackoverflow.com/questions/9240680/how-to-move-3d-object-in-xna?rq = 1) – MARK