This is a very simple question but after trying several time in google i am still not able to fix this.
i have a hero which walk left and right if user hold down the button and change position 5 per frame.
Now what i want is if i press a key single time (not holding) then it will go/walk normally to a fix distance( lets say x +=50 ) and stop there.
here is my code
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
var dPressed:Boolean = false;
var aPressed:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandaler);
stage.addEventListener(KeyboardEvent.KEY_UP , KeyUpHandaler);
stage.addEventListener(Event.ENTER_FRAME , gameLoop);
function keyDownHandaler(Devent:KeyboardEvent):void
{
if (Devent.keyCode == Keyboard.D)
{
dPressed = true;
}
else if (Devent.keyCode == Keyboard.A)
{
aPressed = true;
}
}
function KeyUpHandaler (Uevent:KeyboardEvent):void
{
if (Uevent.keyCode == Keyboard.D)
{
dPressed = false;
hero.gotoAndStop("hero Stand");
}
else if(Uevent.keyCode == Keyboard.A)
{
aPressed = false;
hero.gotoAndStop("hero Stand");
}
}
function gameLoop(Levent:Event):void
{
if (dPressed)
{
hero.x += 5;
hero.gotoAndStop("hero Move Right");
}
else if(aPressed)
{
hero.x -= 5;
hero.gotoAndStop("heroMove Left");
}
}
set up another property:
var counter: uint = 0;
Put a counter in your gameLoop function like this:
if (dPressed == true) {
counter++;
hero.x += 5;
hero.goToAndStop("hero Move Right");
if (counter >= 10){
dPressed = false;
counter = 0;
}
}
Related
I have a life bar in my game and whenever the minion doesn't catch the bananas, the life bar decreases. It works fine, but the problem is that the life bar decreases before it touches the ground. Do I need to add another hitTest in the ground and have it in an && statement?
Main program:
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
var downKey:Boolean;
var jump:Boolean = false;
var xvelocity:int = 9;
var yvelocity:int = 3;
var gravity:Number = 7;
stage.addEventListener(Event.ENTER_FRAME, changeVelocity);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
function changeVelocity(evt:Event){
moveMinion();
yvelocity += gravity;
}
function moveMinion(){
if (leftKey == true){
sideMinion.x -= xvelocity;
sideMinion.left();
}
if (rightKey == true){
sideMinion.x += xvelocity;
sideMinion.right();
}
}
function checkKeyDown(e:KeyboardEvent){
if (e.keyCode == Keyboard.LEFT){
leftKey = true;
}
else if (e.keyCode == Keyboard.RIGHT){
rightKey = true;
}
}
function checkKeyUp(e:KeyboardEvent){
if (e.keyCode == Keyboard.LEFT){
leftKey = false;
}
else if (e.keyCode == Keyboard.RIGHT){
rightKey = false;
}
}
btnStart.addEventListener(MouseEvent.CLICK, makeItFall);
function makeItFall(e:MouseEvent){
var numBananas = 6;
var theBananas: Array = new Array();
theBananas = [];
for (var i = 0; i < numBananas; i++) {
var aBanana: banana = new banana();
theBananas.push(aBanana);
btnStart.visible=false;
aBanana.y=100;
theBananas[i].setSpot(Math.random()*450,Math.random()*200);
theBananas[i].setSpeed((Math.random()), 1);
stage.addChild(aBanana);
}
var health: Number= 100;
var healthPercent: Number= 1;
addEventListener(Event.ENTER_FRAME, pickUpBananas);
function pickUpBananas(event:Event){
for (var i:int = theBananas.length-1; i>-1; i--){
if (sideMinion.hitTestObject(theBananas[i])){
stage.removeChild(theBananas[i]);
theBananas.splice(i,1);
}
else if (sideMinion.hitTestObject(theBananas[i])==false){
health=health-10;
healthPercent=health/100;
trace(healthPercent);
life.scaleX= healthPercent;
}
}
}
}
stop();
And banana class:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.Event;
public class banana extends MovieClip {
var velX: Number = 0;
var velY: Number = 0;
var falling: Boolean = false;
var gravity: Number = 2;
public function banana() {
var timing: Timer = new Timer(25, 0);
timing.addEventListener(TimerEvent.TIMER, moveMe);
timing.start();
}
private function moveMe(event: TimerEvent)
{
x += velX;
y += velY;
if (falling) velY += gravity;
/* trace("[BANANA] position:", x, y, "speed:", velX, velY);*/
}
public function setSpeed(dx,dy) {
velX = dx;
velY = dy;
}
public function setSpot(atX,atY){
this.x=atX;
this.y=atY;
}
public function makeItFall (){
falling=true;
}
}
}
Your problem is basically in the else block. You say if the banana hits, do x, else do y. That's the problem. You can keep your first part if banana hits the minion, do x and then add if banana hits bottom do y.
Get rid of the else.
Hope that made sense.
Edit:
if (the banana is hitting the minion){
// add health or whatever
// remove banana from array
return;
}
if (the banana hits the floor){
// lose health or whatever
// remove banana from array
return;
}
The return statement is so that if you remove a banana, you exit that function instead of going on to the next if statement and checking for a banana that has already been removed.
First, sorry to ask this silly question (I am new in AS3). I wasted more then 2 weeks on this problem, and am now posting it here.
I am making a hero move with the keyboard. I have three animations.
Standby mode
walk front
walk behind
It's working well so far, but the problem I'm facing in jumping the player is having to hold down the key to jump. I don't want the player to be required to hold the key to perform jumping.
So I want to play the full MovieClip with one key press, and honestly I don't know which function I have to use or how to do it.
Here is the file, and here is my code
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
kim.gotoAndStop("kim Stand");
var grav:int = 0;
var floor = 450;
var dPressed:Boolean = false;
var aPressed:Boolean = false;
var jumping:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandaler);
stage.addEventListener(KeyboardEvent.KEY_UP , KeyUpHandaler);
stage.addEventListener(Event.ENTER_FRAME , gameLoop);
function keyDownHandaler(Devent:KeyboardEvent):void
{
if (Devent.keyCode == Keyboard.D)
{
dPressed = true;
}
else if (Devent.keyCode == Keyboard.A)
{
aPressed = true;
}
else if (Devent.keyCode == Keyboard.W && !jumping)
{
jumping = true;
}
}
function KeyUpHandaler (Uevent:KeyboardEvent):void
{
if (Uevent.keyCode == Keyboard.D)
{
dPressed = false;
kim.gotoAndStop("kim Stand");
}
else if(Uevent.keyCode == Keyboard.A)
{
aPressed = false;
kim.gotoAndStop("kim Stand");
}
else if(Uevent.keyCode == Keyboard.W)
{
jumping = false;
kim.gotoAndStop("kim Stand");
}
}
function gameLoop(Levent:Event):void
{
if (dPressed)
{
kim.x += 5;
kim.gotoAndStop("kim Move Right");
}
else if(aPressed)
{
kim.x -= 5;
kim.gotoAndStop("kim Move Left");
}
else if(jumping)
{
kim.gotoAndStop("kim Jump");
kim.y -= 10;
}
gravity();
}
function gravity ():void
{
kim.y += grav;
if (kim.y+kim.height/2 <floor){
grav++;
}
else {
grav = 0;
kim.y = floor - kim.height/2 ;
}
}
You may want to try state based logic. The keyboard events set it, and the actions for updating the character are handled separately in your gameLoop(). The last piece of the puzzle would be to update your state when you recognize you've landed (something that happens not from keyboard interaction, but rather from your gravity function).
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
var grav:int = 0;
var floor = 450;
var state:String = "stand";
stage.addEventListener(KeyboardEvent.KEY_DOWN , keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP , keyHandler);
stage.addEventListener(Event.ENTER_FRAME , gameLoop);
function keyHandler(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.D:
state = (e.type == "keyDown") ? "right" : "stand";
break;
case Keyboard.A:
state = (e.type == "keyDown") ? "left" : "stand";
break;
case Keyboard.W:
state = (e.type == "keyDown") ? "jumping" : "falling";
break;
}
}
function gameLoop(Levent:Event):void {
switch (state) {
case "stand":
kim.gotoAndStop("kim Stand");
break;
case "right":
kim.x += 5;
kim.gotoAndStop("kim Move Right");
break;
case "left":
kim.x -= 5;
kim.gotoAndStop("kim Move Left");
break;
case "jumping":
kim.y -= 10;
kim.gotoAndStop("kim Jump");
break;
case "falling":
kim.gotoAndStop("kim Jump");
break;
}
gravity();
}
function gravity ():void {
kim.y += grav;
if (kim.y + kim.height/2 < floor) {
grav++;
} else {
grav = 0;
kim.y = floor - kim.height/2;
if (state == "falling") {
state = "stand"
}
}
}
I am really new to actionscript and am having an issue with a project I am working on for school. The code that makes my character move (Right, Left, Up, Down) all worked fine but then I added a new Keyframe at the beginning of the project and created a scene to choose a character. I have set it up exactly like my teacher showed us but now that the new keyframe is there, I click the character and it goes to the gameplay but the character cannot be moved. Any help would be very much appreciated.
Here is the code from the first key frame:
stop ();
mc_fox.visible = false;
import flash.events.Event;
import flash.events.MouseEvent;
btn_fox.addEventListener(MouseEvent.CLICK,btn_foxHandler);
function btn_foxHandler(event:MouseEvent):void
{
gotoAndStop(2);
}
And The code from frame 2:
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
mc_fox.stop ();
var move:uint = 0;
stage.addEventListener (KeyboardEvent.KEY_DOWN, keydownHandler);
stage.addEventListener (KeyboardEvent.KEY_UP, keyupHandler);
function keyupHandler (event:KeyboardEvent) :void {
if (event.keyCode == Keyboard.RIGHT){
move = 0
mc_fox.gotoAndStop (1);
}
else if (event.keyCode == Keyboard.LEFT){
move = 0
mc_fox.gotoAndStop (15);
}
else if (event.keyCode == Keyboard.UP){
move = 0
mc_fox.gotoAndStop (30);
}
else if (event.keyCode == Keyboard.DOWN){
move = 0
mc_fox.gotoAndStop (45);
}
}
function keydownHandler (event:KeyboardEvent) :void {
if(event.keyCode == Keyboard.RIGHT && mc_fox.x < 889) {
if (move ==0){
mc_fox.gotoAndPlay (1);
move = 1
}
else {
mc_fox.x = mc_fox.x + 5;
mc_fox.play ();
}
}
else if(event.keyCode == Keyboard.LEFT && mc_fox.x > 111) {
if (move ==0){
mc_fox.gotoAndPlay (15);
move = 1
}
else {mc_fox.x = mc_fox.x - 5;
mc_fox.play ();
}
}
else if (event.keyCode == Keyboard.UP && mc_fox.y > 270) {
if (move ==0){
mc_fox.gotoAndPlay (30);
move = 1
}
else{mc_fox.y = mc_fox.y - 5;
mc_fox.width = mc_fox.width - .9;
mc_fox.height = mc_fox.height - .9;
mc_fox.play();
}
}
else if (event.keyCode == Keyboard.DOWN) {
if (move ==0){
mc_fox.gotoAndPlay (45);
move = 1
}
else{mc_fox.y = mc_fox.y + 5;
mc_fox.width = mc_fox.width + .9;
mc_fox.height = mc_fox.height + .9;
mc_fox.play();
}
}
}
i am new to this whole as3 thing and i am struggling immensely. I have been sat for the last two days trying to do something i can imaging to be simple to everyone else reading this. I am trying to create a game where i have a skate boarder controlled by the keyboard keys. However when i type this code in i am getting a 1084 error please help before i throw my laptop out the window. Thanks!!
package {
import flash.display.*;
import flash.events.*;enter code here
public class skatefate extends MovieClip {
var the_skater:Sprite = new Sprite();
the_skater.addChild:(skater);
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveskater);
function keyPressedDown(event:KeyboardEvent) {
if (event.keyCode == 37) {
moveLeft = true;
} else if (event.keyCode == 39) {
moveRight = true;
} else if (event.keyCode == 65) {
moveUp = true;
} else if (event.keyCode == 90) {
moveDown = true;
}
}
function keyPressedUp(event:KeyboardEvent) {
if (event.keyCode == 37) {
moveLeft = false;
} else if (event.keyCode == 39) {
moveRight = false;
} else if (event.keyCode == 65) {
moveUp = false;
} else if (event.keyCode == 90) {
moveDown = false;
}
}
function moveskater(event:Event) {
var speed:uint = 20;
if (moveLeft) {
skater.x -= speed;
if (skater.x < 0){
skater.x = 800;
}
}
}
if (moveRight) {
skater.x += speed;
if (skater.x > 800){
skater.x = 0;
}
}
if (moveUp) {
skater.y -= speed;
if (skater.y > 0){
skater.y = 0;
}
}
if (moveDown) {
skater.y += speed;
if (skater.y > 0){
skater.y = 0;
}
}
I tried your code but didn't get your error. Your sample throws other errors & problems.
So my advice is these two things..
The correct to construct your code...
package
{
//IMPORTS go here
//Declare your Class
public class skatefate extends MovieClip
{
//VARS go here
//*******************************************************************
//note: later you may also add other VARS inside functions as needed
//(but were not originally put (declared) in this section)
//*******************************************************************
//Declare main function of your Class (must have same name as Class (.as)
public function skatefate()
{
//Constructor code here
//************************************************************************
// Your main program code and related functions (K/board etc) go here and
// will reference your VARS declared above in public Class construction)
//************************************************************************
} //End of (public) Function
} //End of (public) Class
} //End of Package
Just incase you still struggle, this edit of your code shown should compile. From there you can study & learn. Hopefully one more laptop will survive in this cruel world.
package
{
import flash.display.*;
import flash.events.*; //enter code here
//Declare your Class
public class skatefate extends MovieClip {
var the_skater:Sprite = new Sprite();
var skater:Sprite = new Sprite(); //hide line if skater exists already (i.e in Library)
var speed:uint = 20;
//Declare main function of your Class
public function skatefate ()
{
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveskater);
the_skater.addChild(skater);
addChild(the_skater); //adds to stage
function keyPressedDown (event:KeyboardEvent)
{
if (event.keyCode == 37) { moveLeft = true; }
else if (event.keyCode == 39) { moveRight = true; }
else if (event.keyCode == 65) { moveUp = true; }
else if (event.keyCode == 90) { moveDown = true; }
}
function keyPressedUp (event:KeyboardEvent)
{
if (event.keyCode == 37) { moveLeft = false; }
else if (event.keyCode == 39) { moveRight = false; }
else if (event.keyCode == 65) { moveUp = false; }
else if (event.keyCode == 90) { moveDown = false; }
}
function moveskater(event:Event)
{
//var speed:uint = 20; //already declared at top
//speed = 20; // later change 'speed' this way by updating number
if (moveLeft) {
skater.x -= speed;
if (skater.x < 0)
{ skater.x = 800; }
}
if (moveRight) {
skater.x += speed;
if (skater.x > 800)
{ skater.x = 0; }
}
if (moveUp) {
skater.y -= speed;
if (skater.y > 0)
{ skater.y = 0; }
}
if (moveDown) { skater.y += speed;
if (skater.y > 0)
{ skater.y = 0; }
}
} //close 'moveskater' function
} //End of your (public) Function
} //End of your (public) Class
} //End of Package
Hope it helps. Ask for advice in the comments and don't forget to tick as "correct answer" if it works for you. That's how we say "Thanks" on Stack Overflow.
I'm making a platform game. Im trying just to get the player to move around on the stage, and to be able to jump, with some type of gravity added to it. However, when I run it, I get this error: Error #1009: Cannot access a property or method of a null object reference.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Code1 extends MovieClip {
var charSpeed:int = 0;
var velocity:int = 0;
var gravity:Number = 1;
var Jump:Boolean = false;
var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
private var platform:Platform;
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function Code() {
}
public function update(evt:Event){
moveChar();
}
public function moveChar(){
if (leftKey == true){
charSpeed -= 10;
}
if (rightKey == true){
charSpeed += 10;
}
if (upKey == true){
if(!Jump){
velocity -= 14;
Jump = true;
}
}
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = true;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = true;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = true;
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = false;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = false;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = false;
}
}
function loop(evt:Event){
player.x = charSpeed;
if (player.x < 0){
player.x = 0;
}
if (player.x > 550){
player.x = 550;
}
velocity += gravity;
if (!platform.hitTestPoint(player.x, player.y, true)){
player.y += velocity;
}
for (var i = 0; i < 10; i++){
if (platform.hitTestPoint(player.x, player.y, true)){
player.y--;
velocity = 0;
Jump = false;
}
}
}
}
}
My platform linkage is "Platform", but i set up a variable for it (or tried to). I debugged the code, and it came up with this line: player.x = charSpeed;
I have no idea what to do, if someone could help, that would be great.
You never declare or instantiate (ie player = new Player()) your player.
Alternatively, if your player is on the stage of your .fla timeline, it will need the instance name 'player'. That can be set in the object properties.
You player object is null.
I don't see the istantiation line as:
var player:Player = new Player();
Add it before use a property of your player