After a well deserved break we have started up with programming. Instead of placing our objects on the stage in Flash like we did in the first three cases, we are to have a blank screen and load our objects in using Action Script. We were also to have a character that could be controlled with the arrowkeys.
We were given all the code we needed to finish the assignment with ease, though there was quite a bit of tweeking needed to make it look good.
Instead of throwing myself at animating Shaph, I decided to get the heavy work out of the way first, and tried my hand with coding a simple stickman animation. A good choice, as I encountered a bit of trouble. When coding like this, one mistake was enough to muck up the result and make it so the file appeared completely blank.
I also encountered a problem with the character animation. The code we were given beforehand included instructions for how to make the walk animation play while the character walked left, but not when the character walked towards right. I figured out how to make the animation play, but missing out on a bracket messed it all, so that when I added the right animation, it refused to play the left animation. Easy enough, but the problem was that Flash didn't alert me that there was an error in my code(I also have found a better site to host my swfs, but Flash still refuses to let them work when I embed them).
We were given all the code we needed to finish the assignment with ease, though there was quite a bit of tweeking needed to make it look good.
Instead of throwing myself at animating Shaph, I decided to get the heavy work out of the way first, and tried my hand with coding a simple stickman animation. A good choice, as I encountered a bit of trouble. When coding like this, one mistake was enough to muck up the result and make it so the file appeared completely blank.
I also encountered a problem with the character animation. The code we were given beforehand included instructions for how to make the walk animation play while the character walked left, but not when the character walked towards right. I figured out how to make the animation play, but missing out on a bracket messed it all, so that when I added the right animation, it refused to play the left animation. Easy enough, but the problem was that Flash didn't alert me that there was an error in my code(I also have found a better site to host my swfs, but Flash still refuses to let them work when I embed them).
Yes, it's a simple and pathetic animation, but it was more than enough coding. I have included it for fun's sake.
// TestMain.as RJA 2010
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
public class TestMain extends MovieClip
{
private var forgrunn:Forgrunn;
private var karakter:Karakter
const L:int = 0; //Left
const W:int = 550; //Width
const T:int = 0; //Top
const H:int = 400 //Height
const R:int = L+W //Right
const B:int = T+H //Bottom
public function TestMain()
{
forgrunn = new Forgrunn ();
addChild (forgrunn);
forgrunn.x = 0
forgrunn.y = B - forgrunn.height
karakter = new Karakter ();
addChild (karakter);
karakter.x = R/2
karakter.y = forgrunn.y - forgrunn.height/2 - 160
karakter.gotoAndStop("idle");
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
}
public function onKeyPressed(e:KeyboardEvent) : void
{
if (e.keyCode == Keyboard.LEFT)
{
karakter.x = karakter.x -5;
karakter.gotoAndStop("left");
if (karakter.x <= 0) { (karakter.x = 5) karakter.gotoAndStop("idle") } } else if (e.keyCode == Keyboard.RIGHT) { karakter.x = karakter.x +5; karakter.gotoAndStop("right"); if (karakter.x >= 535)
{
(karakter.x = 530)
karakter.gotoAndStop("idle")
}
}
}
public function onKeyReleased(e:KeyboardEvent) : void
{
karakter.gotoAndStop("idle")
}
}
}
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
public class TestMain extends MovieClip
{
private var forgrunn:Forgrunn;
private var karakter:Karakter
const L:int = 0; //Left
const W:int = 550; //Width
const T:int = 0; //Top
const H:int = 400 //Height
const R:int = L+W //Right
const B:int = T+H //Bottom
public function TestMain()
{
forgrunn = new Forgrunn ();
addChild (forgrunn);
forgrunn.x = 0
forgrunn.y = B - forgrunn.height
karakter = new Karakter ();
addChild (karakter);
karakter.x = R/2
karakter.y = forgrunn.y - forgrunn.height/2 - 160
karakter.gotoAndStop("idle");
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
}
public function onKeyPressed(e:KeyboardEvent) : void
{
if (e.keyCode == Keyboard.LEFT)
{
karakter.x = karakter.x -5;
karakter.gotoAndStop("left");
if (karakter.x <= 0) { (karakter.x = 5) karakter.gotoAndStop("idle") } } else if (e.keyCode == Keyboard.RIGHT) { karakter.x = karakter.x +5; karakter.gotoAndStop("right"); if (karakter.x >= 535)
{
(karakter.x = 530)
karakter.gotoAndStop("idle")
}
}
}
public function onKeyReleased(e:KeyboardEvent) : void
{
karakter.gotoAndStop("idle")
}
}
}
I have already gotten to working on my actual product with Shaph getting to be moved aorund, but I am still trying to work out some bugs. I am hoping to be able to flip the idle animation so he won't turn around to face right after having just walked left. We'll see about it and I'll post more soon.
![]() |
| A better stickman |

No comments:
Post a Comment