Archive for November 3, 2010

Case 4 – Programming 1

Posted in Cases on November 3, 2010 by alllifeme

In this “case” we were asked to create the main scene in our game/entertainment production using the graphic and character we made in the 3 first  cases. But this had to be done by placing them through ActionScript 3.0.
We also had to make the character move  left and right and prevent it from walking out of the screen.

What I did:

Our teacher showed us a a script that we could use as a start for our own game. I used that, changing the names I needed to change.

Here is the code: (Bolded parts are my names and things I added myself)

// InteraktivScene1Main.as – ANI121 – DN 271010
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.display.BitmapData;

public class AS_tryout_1_Main extends MovieClip
{
private var gress1:gress;
private var karakter:gå_høyre;
private var bakgrunn:Bakgrunn;

//  Bounding box
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 AS_tryout_1_Main()
{
bakgrunn = new Bakgrunn();
addChild(bakgrunn);
bakgrunn.x = 275;
bakgrunn.y = 200;

gress1 = new gress();
addChild(gress1);
gress1.x = 225;
gress1.y = 340;

karakter = new gå_høyre();
addChild(karakter);
karakter.x = 125;
karakter.y = 380;
karakter.gotoAndStop(1);
karakter.width = 100;
karakter.height = 100;

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 > L)
{
karakter.x = karakter.x – 2;
karakter.scaleX = -0.28;
}
else if (e.keyCode == Keyboard.RIGHT && karakter.x < R) {
karakter.x = karakter.x + 2;
karakter.play();
karakter.scaleX = 0.28;
}

}
public function onKeyReleased(e:KeyboardEvent) : void
{
karakter.gotoAndStop(1);
}
}
}

But it didn’t work to my satisfaction, mainly bacause the movement. With this code, when I pressed end held the mouse button, the character moved first the amount of pixles I said it should, then stopped for a half a second or a second, and moved like it should after that. A classmate, Andreas Hellesvik, solved this with boolean variables. But because his script was quite different from the script I had, he let me copy his script. I changed the names I needed for it to work with my graphic/objects, size of the stage, The pace of my characters movement.

This is my script now:

// AS_tryout_Main_2 – JF 117083

package  {

import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;

public class AS_tryout_Main_2 extends MovieClip {

private var forgrunn:gress;
private var himmel:Bakgrunn;
private var tre:animatedgrass;
private var sky:cloud_1;
private var spritemain:spritemain1;
private var leftKey:Boolean = false
private var rightKey:Boolean = false


const L:int = 0;
const W:int = 550;
const T:int = 0;
const H:int = 400;
const R:int = L+W;
const B:int = T+H;

public function AS_tryout_Main_2() {

himmel = new Bakgrunn();
addChild(himmel);
himmel.x = 275;
himmel.y = 200;


forgrunn = new gress();
addChild(forgrunn);
forgrunn.x = 225;
forgrunn.y = 340;


sky = new cloud_1();
addChild(sky);
sky.x = 300;
sky.y = 50;


tre = new animatedgrass();
addChild(tre);
tre.x = 400;
tre.y = 330;


spritemain = new spritemain1();
addChild(spritemain);
spritemain.x = 130;
spritemain.y = 370;
spritemain.height = 100;
spritemain.width = 100;
spritemain.gotoAndStop(1);

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);
}

public function onFrameLoop(e:Event) : void
{

sky.x = sky.x – 1;
run();
}

public function onKeyPressed(e:KeyboardEvent) : void
{

if (e.keyCode == Keyboard.LEFT) {

leftKey = true;
}


else if (e.keyCode == Keyboard.RIGHT) {

rightKey = true ;
}
}
public function onKeyReleased(e:KeyboardEvent) : void {

if(e.keyCode == Keyboard.LEFT) {

leftKey = false;
spritemain.gotoAndStop(1)
}

if(e.keyCode == Keyboard.RIGHT) {

rightKey = false;
spritemain.gotoAndStop(1)
}
}

public function run() : void
{

if(leftKey == true) {

spritemain.x-=3;
spritemain.scaleX=-0.28;
spritemain.gotoAndStop(2);
}

if(spritemain.x<15){spritemain.x+=3}
if(rightKey == true) {

spritemain.x+=3;
spritemain.scaleX=0.28;
spritemain.gotoAndStop(2);
}

if(spritemain.x>535)
{
spritemain.x-=3;
}

}

}

}

 

What I have learned:
I have learned that addChild is used to add something to the stage. I have learned about sprites, that it is a great way to add the different movements so that the overall size of the file is smaller. And a great way to easily go to the movements when needed.

I already knew some scripting before I began this, but I still have dificulty understanding everything when put together.