ForumsProgramming ForumGame in the works

20 19179
Fortheseen
offline
Fortheseen
12 posts
Nomad

Hey all, I am somewhat new to programming and I have basic start of a video game. I have been working on Adobe Flash Professional CS6 using AS3 and I am still trying to figure some things out here. My main problem is at the moment is getting my animations to work with my button presses and I notice sometimes when I press those buttons they do not work at the moment they're pressed and I know it has something to do with the timers but if I remove the timers the animations over lap each other when buttons are pressed. Here is what I got so far with my code: By the way Jump, Run, & Slide have the exact same code expect for class and function name.

package Action {

import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;

public class Jump extends MovieClip {

private var stageRef:Stage;

public function Jump(stageRef:Stage, x:Number, y:Number) {
this.stageRef = stageRef;
this.x = 200;
this.y = 600;

addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}

private function loop(e:Event) {
if (currentFrame == totalFrames)
removeSelf();
}

private function removeSelf() : void {
removeEventListener(Event.ENTER_FRAME, loop);

if (stageRef.contains(this))
stageRef.removeChild(this);
}

}

}

package Action {

import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;

public class Runner extends MovieClip {

private var ourChar:Start;

public function Runner() {
//create an object of our character from the Run class
ourChar = new Start(stage);

//add it to the display list
stage.addChild(ourChar);

//starting location of character
ourChar.x = stage.stageWidth / 8;
ourChar.y = stage.stageHeight / 1.15;
}
}

}

package Action {

import flash.display.MovieClip;
import flash.display.Stage;
import Input.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class Start extends MovieClip {

private var stageRef:Stage;
private var key:KeyObject;
private var JumpTimer:Timer;
private var canJump:Boolean = false;
private var SlideTimer:Timer;
private var canSlide:Boolean = false;
private var RunTimer:Timer;
private var canRun:Boolean = false;
private var jump:Boolean = false;
private var gravity:Number = 10;
private var jumpPower:Number = 0;
private var isJumping:Boolean = false;
private var ground:Number = 600;

public function Start(stageRef:Stage) {
this.stageRef = stageRef;
key = new KeyObject(stageRef);

JumpTimer = new Timer(1000, 1);
JumpTimer.addEventListener(TimerEvent.TIMER, jumpTimerHandler, false, 0, true);

SlideTimer = new Timer(1000, 1);
SlideTimer.addEventListener(TimerEvent.TIMER, slideTimerHandler, false, 0, true);

RunTimer = new Timer(1000, 1);
RunTimer.addEventListener(TimerEvent.TIMER, runTimerHandler, false, 0, true);

addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}

public function loop(e:Event) : void {
//key presses
if (key.isDown(Keyboard.UP) && !canJump && !canRun) {
if(!isJumping) {
Jumper();
}
} else if (key.isDown(Keyboard.DOWN) && !canSlide && !canRun) {
Slider();
} else {
Runnerz();
}
if(isJumping) {
this.y -= jumpPower;
jumpPower -= 2;
}

if(this.y + gravity < ground) {
this.y += gravity;
} else {
this.y = ground;
isJumping = false;
}
}

private function Jumper() : void {
if (!canJump) {
stageRef.addChild(new Jump(stageRef, x, y));
canRun = true;
canJump = true;
canSlide = true;
this.jumpPower = 30;
isJumping = true;
}
}

private function Slider() : void {
if (!canSlide) {
stageRef.addChild(new Slide(stageRef, x, y));
canSlide = true;
canRun = true;
canJump = true;
}
}

private function Runnerz() : void {
if (!canRun) {
stageRef.addChild(new Run(stageRef, x, y));
canRun = true;
}
RunTimer.start();
}

private function jumpTimerHandler(e:TimerEvent) : void {
canJump = false;
}

private function slideTimerHandler(e:TimerEvent) : void {
canSlide = false;
}

private function runTimerHandler(e:TimerEvent) : void {
canRun = false;
canSlide = false;
canJump = false;
}

}

}

I would gladly accept your help and thank you for looking over the code.

  • 20 Replies
weirdlike
offline
weirdlike
1,299 posts
Prince

Skimming through the code it looks like when you keypress, you addChild then as the clip is finished you removeChild.

IMO this will cause a lot of errors. Instead you can create a movieClip and have all the sequences of movieClips in frames and label the frames to reference the sequence. I would also make the keypress in a separate class using switch, which will be dedicated to booleans in the main document class, and from there check if the key is pressed.

I have made a few sample's of what I am describing. My most current example HERE on setting up flash. Also a sample of game play HERE

I suggest to skim through it and pick out what you want. I can put together a "simple" sample of the movieClip sequences I briefly suggested If you want.

weirdlike
offline
weirdlike
1,299 posts
Prince

well I went ahead and made it. I kinda slapped it together so don't expect it to be perfect. I also put everything in the actions panel, but you should be able to easily pick out what you want.

LINK

Fortheseen
offline
Fortheseen
12 posts
Nomad

Hey, thanks for getting back to me. I have been trying to convert my work into action panels and I haven't really used them before. I looked over the work you did with the LINK and I wanted to ask how does the compiler know what the defined property for hero is? I didn't notice one when I looked through it.

weirdlike
offline
weirdlike
1,299 posts
Prince

I highly suggest continuing working with the classes as that is what I prefer most.

As you load the file you can see the "hero" on the screen. Take a look at the timeline and you will see the layer containing the hero. It might be a locked layer just click on the lock to allow changes on the layer then you can freely select the hero. At that point while the hero is selected click on properties and you will see that hero is the Instance name. (if you don't see the properties panel press Ctrl+F3)

Normally in classes you just do this

var hero:heroMC = new heroMC();
addChild(hero);

The instance name is hero while the AS linkage is heroMC but on the timeline you need to declare the instance name by typing into that box and there is no addChild as it already exists on the screen.

EDIT: double-click on the heroMC to open an internal timeline showing the sequences.

weirdlike
offline
weirdlike
1,299 posts
Prince

I may have misunderstood your question. Flash automatically generates the definition on export. With the clip on screen you can convert to symbol. Then the properties box will ask if you want to export for action script, if selected the base class definition is right below it. If you decide you want to create your own "hero" class then you will need to "extend movieClip".

http://i.imgur.com/F7KzeeU.jpg

Fortheseen
offline
Fortheseen
12 posts
Nomad

I finally figured it out and thanks again for the help. Do you know how to make it when I press a button it automatically changes to the other animation? I seem to have a minor lag between animations with button presses. I have it set to 24 frames per second and I am using exactly that amount so I wasn't sure if that was it.

weirdlike
offline
weirdlike
1,299 posts
Prince

hmm.. well post what you have and I can look it over.

Fortheseen
offline
Fortheseen
12 posts
Nomad

This is what I have so far for my animations:

import flash.events.Event;

hero.gotoAndStop("running&quot;


var upPressed:Boolean = false;
var downPressed:Boolean = false;

var gravity:Boolean = true;
var jumpTrue:Boolean = false;
var groundFix:Boolean = false;
var jumpHeight:Number = 0;

hero.addEventListener(Event.ENTER_FRAME, heroMove);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUnPressed);

function heroMove(event:Event) {
if (upPressed) {
if (jumpTrue) {
if (groundFix) {
jumpHeight = hero.y - 75;
groundFix = false;
hero.y -= 15;
} else {
hero.y -= 15;
if(hero.y

Fortheseen
offline
Fortheseen
12 posts
Nomad

[lesser than equal sign] jumpHeight) {
jumpTrue = false;
}
}
}
}
if (gravity) {
hero.y += 5;
}
}

function keyPressed(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP: {
if (upPressed) {
hero.gotoAndStop("jumping&quot;
}
upPressed = true;
break;
}
case Keyboard.DOWN: {
if (downPressed) {
hero.gotoAndStop("sliding&quot;
} else {
hero.gotoAndStop("running&quot;
}
downPressed = true;
break;
}
}
}

function keyUnPressed(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP: {
if (downPressed) {
upPressed = false;
jumpTrue = false;
stop();
hero.gotoAndStop("jumping&quot;
} else {
upPressed = false;
jumpTrue = false;
hero.gotoAndStop("running&quot;
}
break;
}
case Keyboard.DOWN: {
if (upPressed) {
downPressed = false;
hero.gotoAndStop("sliding&quot;
} else {
downPressed = false;
hero.gotoAndStop("running&quot;
}
break;
}
}
}

Not sure why that didn't want to post.

weirdlike
offline
weirdlike
1,299 posts
Prince

Well looking at the code is limited when the question is directed at the animation. I am speculating that you might need to look at the frames in the clip that has the delay. First make a copy of it then you can try removing the first frame. If it is exactly the same as clip before it. Then it makes sense that it will show the same frame (and appear to have slight lag) then continue through the timeline. If you notice that it is making a difference then you can play around with the animation and find the exact frame to start from.

Fortheseen
offline
Fortheseen
12 posts
Nomad

Okay, I'll mess around with the frames and see what's going on with them. Thanks for the help again.

Fortheseen
offline
Fortheseen
12 posts
Nomad

Would the cause of the problem be if I am using the same picture on multiple frames?

weirdlike
offline
weirdlike
1,299 posts
Prince

It could very well be it. When you hero.gotoAndStop("jumping&quot; and there is a slight lag but only at the beginning then it might not be a lag at all, and it is the start of the sequence. Remove the frames (after you make a copy) that are at the beginning until it is smooth.

Fortheseen
offline
Fortheseen
12 posts
Nomad

Is there a command or something to completely stop the previous animation? While the other animation is running?

weirdlike
offline
weirdlike
1,299 posts
Prince

It is to my understanding when you switch frames the MovieClip on the previous frame ends and when you switch back it starts over. Anywho... The hero movie clip is on the stage. So the stage is the parent and hero is the child (unless you addChild on some other layer or it is not already present on stage). The hero (if you double click on the clip) also contains children; running, jumping, and sliding. You can select these children and give them instance names. If the jumping clip was declared as jumpingMC then you could use hero.jumpingMC.stop(); or hero.jumpingMC.gotoAndStop(1);. If you create a hero class you would eliminate the hero bit all together and it would look like this jumpingMC.gotoAndStop(1); If you decide to take this route you might also need to use hero.jumpingMC.play();

Showing 1-15 of 20