ForumsProgramming ForumGame in the works

20 19180
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
Fortheseen
offline
Fortheseen
12 posts
Nomad

Okay, thanks.

Fortheseen
offline
Fortheseen
12 posts
Nomad

Hey, sorry to bother you again but I am trying to add in a start button and I want to change scenes after I click on it. This is the code I have for my button:

import flash.events.MouseEvent;

Object(this).start.addEventListener(MouseEvent.CLICK, goStart);

function goStart(event:MouseEvent):void {
gotoAndStop(2, "Scene 2&quot;
}

I am not sure why it won't go to the second scene.

Fortheseen
offline
Fortheseen
12 posts
Nomad

Actually never mind I figured it out, lol... I guess I do have another question I noticed when I change scenes I have to click on the game box again to start moving my character is that normal when someone does that?

weirdlike
offline
weirdlike
1,299 posts
Prince

It has to do with the focus, like if there was a text box that entered the name for high scores you can take control by using focus, but for focusing on the game you use this

stage.stageFocusRect = false;
stage.focus = YOURGAMEHERE;

Fortheseen
offline
Fortheseen
12 posts
Nomad

Hey, thanks for the help by the way. It sounds like my friends and I are going to convert this all into Unity and work with that more instead of adobe flash.

Showing 16-20 of 20