VG:Actors (VG2)

From MyProjectFun
Jump to: navigation, search

Adding actors into our game is required for sprites - we can't have a sprite without an actor. An actor is a set of animations. For example, to add an actor for our player, we want to add the following code to MyGame.cs in the CreateResources function, since that is where we want to load files into our game.

Actor Player = new Actor("PlayerActor");

This gives us a new Actor object named PlayerActor, which we keep track of in our Actor variable Player. If we want to access our new actor in this function, we can just use Player. In other functions, we would have to use This.Game.FindActor("PlayerActor") to have the game find our actor.

Now that we have our actor, we want to add animations to it. An actor without animations is not very useful, and could very well crash our game. So, we'll assume we've made an animation with the animation editor called Player.anim. To add this animation to our actor, we use the following code, just under where we made our actor.

Player.AddAnimation( AnimationLoader.load( "Player" ) );

That line of code tells the actor kept in our Player variable to add an animation. The animation we add is the animation object returned by the AnimationLoader's load function, which requires the name of the animation file. Notice that this does not include our .anim - that's because the AnimationLoader knows that we'll be loading .anim files, so it adds it automatically.

In total, our code in the CreateResources function of MyGame.cs looks like this:

Actor Player = new Actor("PlayerActor");
Player.AddAnimation( AnimationLoader.load( "Player" ) );
Personal tools
Namespaces
Variants
Actions
Navigation
Engineering
Game Design
Multimedia Production
VG Programming
Downloads
Toolbox