如何在Goblin XNA中移动物体?

时间:2014-02-24 10:49:16

标签: c# 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

2 个答案:

答案 0 :(得分: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);

    }

答案 1 :(得分:0)

从本质上讲,您所需要的只是一个班级Vector2(位置)和正在绘制的纹理。我确信yourSprite的类包含这些。如果您无法使其工作,请尝试以下方法:

  1. 在班级,定义名为Vector2的{​​{1}}并将其设置为初始位置(即中心屏幕)

  2. 在Update()方法中,使用您发布的代码,但替换

    position

    yourSprite.position
    

    这将更新我们希望角色所在的位置。

  3. position 方法中,再次将Draw()替换为yourSprite.position