as3 error: ReferenceError: Error #1069: Property keyCode not found on flash.display.Shape and there is no default value - actionscript-3

I am following a tutorial on building a rhythm game in as3 here, and I am very new to the language. On running the swf, I get the following error in the output:
ReferenceError: Error #1069: Property keyCode not found on flash.display.Shape and there is no default value.
at source_fla::MainTimeline/makeLvl()[source_fla.MainTimeline::frame10:116]
I have tried some previous solutions posted to the same error, but I have not been able to resolve the issue.
Here is the source code:
stop();
stage.focus = stage;
//VARIABLES
//sTime is the current frame that is being played
//Once it reaches sTempo, then it will be reset
//and a note will be created
var sTime:int = 0;
//sTempo is how many frames it takes before
//a note is created. Because it's 12, and
//the frame rate is 24, it will take a half of a second
//for a note to be made
var sTempo:Number = 12;
//sNote is the current arrow of the level that is created
//0 makes no arrow
//1 makes a left arrow
//2 makes an up arrow
//3 makes a down arrow
//4 makes a right arrow
var sArrow:int = 0;
//arrowSpeed is how fast the arrow moves up the screen
var arrowSpeed:Number = 10;
//gameIsOver is whether the game's over
var gameIsOver:Boolean = false;
//the score variable
var score:int = 0;
//either perfect, great, nice, or good
var scoreString:String = '';
var combo:int = 0;
var mcHealth:Number = 0;
//Booleans checking if the arrows are touching the receptor
var touchLeft:Boolean = false;
var touchUp:Boolean = false;
var touchDown:Boolean = false;
var touchRight:Boolean = false;
function beginCode():void{
addEventListener(Event.ENTER_FRAME, makeLvl);
//make the level array longer
lvlArrayAll[lvlCurrent].push(0,0,0,0,0);
}
function makeLvl(e:Event):void{
//code here will create the level
if(sTime < sTempo){
//if the required time hasn't reached the limit
//then update the time
sTime ++;
} else {
//if the time has reached the limit
//then reset the time
sTime = 0;
//if an actual arrow can be made
if(lvlArrayAll[lvlCurrent][sArrow] != 0){
var currentArrow:MovieClip; //this will hold the current arrow
if(lvlArrayAll[lvlCurrent][sArrow] == 1){
//place a left note onto the stage
currentArrow = new arrowLeft();
//set the _x value of the note so that it is in the
//right place to touch the receptor
currentArrow.x = 105 ;
//set the note's y coordinate off of the stage
//so that the user can't see it when it appears
currentArrow.y = 0;
//setting the key that needs to be pressed
currentArrow.keyCode = 68;
addChild(currentArrow);//add it to stage
} else if(lvlArrayAll[lvlCurrent][sArrow] == 2){
//place an up note onto the stage
currentArrow = new arrowUp();
currentArrow.x = 230;
currentArrow.y = 0;
currentArrow.keyCode = 70;
addChild(currentArrow);
} else if(lvlArrayAll[lvlCurrent][sArrow] == 3){
//place a down note onto the stage
currentArrow = new arrowDown();
currentArrow.x = 355;
currentArrow.y = 0;
currentArrow.keyCode = 74;
addChild(currentArrow);
} else if(lvlArrayAll[lvlCurrent][sArrow] == 4){
//place a right note onto the stage
currentArrow = new arrowRight();
currentArrow.x = 480;
currentArrow.y = 0;
currentArrow.keyCode = 75;
addChild(currentArrow);
}
}
//get the next arrow if it the song isn't finished
if(sArrow < lvlArrayAll[lvlCurrent].length){
sArrow ++;
} else {
//if the song is finished, then reset the game
gotoAndStop('win');
gameIsOver = true;
//then remove this enter_frame listener
removeEventListener(Event.ENTER_FRAME, makeLvl);
}
}
//checking if mcReceptor is touching any arrows
//first we reset the variables we got last time just in case they aren't true anymore
touchLeft = false;
touchUp = false;
touchDown = false;
touchRight = false;
//this for loop will be used for the hit testing
for(var i:int = 0;i<numChildren;i++){
var target:Object = getChildAt(i);
if(target.keyCode != null && target.hitTestObject(mcReceptor)){//if the object is an arrow and the receptor is touching it
if(target.keyCode == 68){//if left arrow
touchLeft = true;
} else if(target.keyCode == 70){//if up arrow
touchUp = true;
} else if(target.keyCode == 74){//if down arrow
touchDown = true;
} else if(target.keyCode == 75){//if right arrow
touchRight = true;
}
}
}
//changing the score text
mcTxt.txtScore.text = 'Score: '+score;
mcTxt.txtCombo.text = 'Combo: '+combo;
mcTxt.txtScoreString.text = scoreString;
}
//this function will change the health depending on how much health change
//it needs, positive or negative
function changeHealth(healthDiff:Number):void{
healthDiff = 100;//only changes in percentages
//checking if the health is already at it's full
//or will be full after this hit
if(mcHealth + healthDiff >= 100){
mcHealth = 100;
} else if(mcHealth + healthDiff <= 0){
//checking if the health will be equal or below 0
gotoAndStop('lose');
gameIsOver = true;
removeEventListener(Event.ENTER_FRAME, makeLvl);
} else {
//if there are no problems
mcHealth += healthDiff;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeys);
function checkKeys(event:KeyboardEvent):void{
//if the left key is down and no left arrows are touching the receptor
if(event.keyCode == 68 && !touchLeft){
changeHealth(-10);//make the health go down
combo = 0;
scoreString = 'Bad';
} else if(event.keyCode == 70 && !touchUp){//and so on
changeHealth(-10);
combo = 0;
scoreString = 'Bad';
} else if(event.keyCode == 74 && !touchDown){
changeHealth(-10);
combo = 0;
scoreString = 'Bad';
} else if(event.keyCode == 75 && !touchRight){
changeHealth(-10);
combo = 0;
scoreString = 'Bad';
}
}
beginCode();
Can someone tell me why this error is occurring? Thank you.

While iterating numChildren, it's necessary to check the object is an arrow or not.
Maybe you can distinguish it by having keyCode property or not.
Try to use Object.hasOwnProperty(property name) method.
if (target.hasOwnProperty("keyCode")){
// access target.keyCode here.
}
Or this might works too.
if (target is arrowLeft || target is arrowUp || target is arrowDown || target is arrowRight){
// the target should be arrow class
// access target.keyCode here.
}
//this for loop will be used for the hit testing
for(var i:int = 0;i<numChildren;i++){
var target:Object = getChildAt(i);
if (target.hasOwnProperty("keyCode")){ // If the object is an arrow, that object should has keyCode property.
if(target.keyCode != null && target.hitTestObject(mcReceptor)){//if the object is an arrow and the receptor is touching it
if(target.keyCode == 68){//if left arrow
touchLeft = true;
} else if(target.keyCode == 70){//if up arrow
touchUp = true;
} else if(target.keyCode == 74){//if down arrow
touchDown = true;
} else if(target.keyCode == 75){//if right arrow
touchRight = true;
}
}
}
}

Related

how to make object move in specific position flash as3

I make an mc object. this object can move right and left with keyboard. details are when I push rightkey it moves to right position in certain coordinate and when I push leftkey it moves to left position in certain coordinate. I want the object just move in 3 position.
for this case, I tried to use array.
var P:Array = [new Point(100, 300), new Point(275, 300), new Point(425, 300)];
var M:Array = [Kotak];
but when I input them to my code, it doesn't work and no error appears. anyone can tell me where's my fault?
this's my full code :
import flash.geom.Point;
//gerak pemain
var pemainKanan:Boolean = false;
var pemainTengah:Boolean = false;
var pemainKiri:Boolean = false;
//kecepatan
var kecepatanPemain:int = 20;
//Array object acak
var P:Array = [new Point(100, 300), new Point(275, 300), new Point(425, 300)];
var M:Array = [Kotak];
//var P:Point = new Point(100, 300);
Kotak.addEventListener(KeyboardEvent.KEY_UP, k);
function k(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT){
pemainKanan = false;
}
if(e.keyCode == Keyboard.LEFT){
pemainKiri = false;
}
}
Kotak.addEventListener(KeyboardEvent.KEY_DOWN, kk);
function kk(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT){
pemainKanan = true;
}
if(e.keyCode == Keyboard.LEFT){
pemainKiri = true;
}
}
Kotak.addEventListener(Event.ENTER_FRAME, eframe);
function eframe(e:Event):void{
if(pemainKanan == true){
//pemain.gotoAndStop("right");
Kotak.x = P[0];
}
else if(pemainKiri == true){
//pemain.gotoAndStop("left");
Kotak = P[1];
}
}
Can you please explain that in detail? From what I've read I understand that you want a MovieClip to move to three positions. For example if it is on position0 and right is pressed it should move to position1, but if it's in position1 it should move to position2 and the other way around with left button. Is this what you want to do? If it is there is no need for the ENTER_FRAME event, since all it can be done maybe with an index.
Kotak.addEventListener(KeyboardEvent.KEY_DOWN, kk);
var index:int=0;
function kk(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT){
if(index==0){
Kotak.x = P[1];
}
else{
if(index==1){
Kotak.x = P[2];
}
}
index++;
}
if(e.keyCode == Keyboard.LEFT){
if(index==2){
Kotak.x = P[1];
}
else{
if(index==1){
Kotak.x = P[0];
}
}
index--;
}
}

Why can't I reset this game? I tried resetting variables etc

I have this flash game coded with AS3. It works on the first round. When you die, the program will loop it back to the game frame in which a couple or errors occur. One. the dead characters from last round still remain still. Two. The speed doubles everytime. Why????
I create a resetEverything() function to reset all variables, remove Event Listeners and a clear.graphics()
Why doesn't anything work?
Here is my code..
stop();
var leftBorder:verticalwall = new verticalwall(); // defining a variable to hold the left wall
addChild(leftBorder); // adding the left wall to the stage
var rightBorder:verticalwall = new verticalwall(); // defining a variable to hold the left wall
rightBorder.x = 790; // pushing the right wall to the edge of the stage
addChild(rightBorder); // adding the right wall to the stage
var topBorder:horizontalwall = new horizontalwall(); // defining a variable to hold the left wall
addChild(topBorder); // adding the top wall to the stage
var bottomBorder:horizontalwall = new horizontalwall(); // defining a variable to hold the bottom wall
bottomBorder.y = 790; // pushing the bottom wall to the base of the stage
addChild(bottomBorder); // adding the bottom wall to the stage
var P1positions:Array = new Array(); //defining a new variable to hold the poistions of Player 1
var P2positions:Array = new Array(); //defining a new variable to hold the poistions of Player 2
graphics.beginFill( 0x000000 ); // defining a colour for the background
graphics.drawRect( 0, 0, 800, 800); // drawing a rectangle for background
graphics.endFill(); // ending the background creating process
stage.addEventListener(Event.ENTER_FRAME, checkPlayersSpeed);
function checkPlayersSpeed(event:Event)
{
if(P1speed == 0 || P2speed == 0){
P1speed = 0;
P2speed = 0;
gotoAndStop(1, "Conclusion");
}
}
//Player 1
var player1col = "0xFF0000";
var Player1:Shape = new Shape(); //defining a variable for Player 1
Player1.graphics.lineStyle(10,0xffff00); //defining the colour of the style
Player1.graphics.beginFill(0xffff00); //begin filling the shape
Player1.graphics.drawRoundRect(0,0,3,3,360);
Player1.graphics.drawCircle(Player1.x, Player1.x, 2.4) //draw a circle
Player1.graphics.endFill(); //finish the filling process
addChild(Player1); //add player 1 to stage
Player1.x = 250;
Player1.y = 250;
var P1leftPressed:Boolean = false; //boolean to check whether the left key for Player 1 was pressed
var P1rightPressed:Boolean = false; //boolean to check whether the right key for Player 1 was pressed
var P1speed = 3.5; //variable to store the speed of which player 1 moves
var P1Dir = 45; //variable containing the direction in which player 1 moves
var P1position, P2position;
Player1.addEventListener(Event.ENTER_FRAME, P1fl_MoveInP1DirectionOfKey); //adding a listener to the player
stage.addEventListener(KeyboardEvent.KEY_DOWN, P1fl_SetKeyPressed); //listener for a key to be pressed
stage.addEventListener(KeyboardEvent.KEY_UP, P1fl_UnsetKeyPressed); // listener for a key to be released
function P1fl_MoveInP1DirectionOfKey(event:Event) //Moves the player depedning on what key was pressed
{
if(Player1.hitTestObject(leftBorder) || Player1.hitTestObject(rightBorder) || Player1.hitTestObject(topBorder) || Player1.hitTestObject(bottomBorder)){ //checking to see whether Player 1 has hit the wall
P1speed = 0; //stopping Player 1 from moving
Player1.removeEventListener(Event.ENTER_FRAME, P1fl_MoveInP1DirectionOfKey); //adding a listener to the player
stage.removeEventListener(KeyboardEvent.KEY_DOWN, P1fl_SetKeyPressed); //listener for a key to be pressed
stage.removeEventListener(KeyboardEvent.KEY_UP, P1fl_UnsetKeyPressed); // listener for a key to be released
}
if (P1leftPressed)
{
P1Dir -= 0.1; //changes the direction to make Player 1 rotate
}
if (P1rightPressed)
{
P1Dir += 0.1; //changes the direction to make Player 1 rotate
}
P1position = [Player1.x, Player1.y]; //defining a variable for Player 1's constant positions
P1positions.push(P1position); //pushes every position of Player 1 to the array
for (var i = 0; i < P1positions.length - 10; i++) { //a loop that opperates for as long as the array is receiving positions
var P1x = P1positions[i][0]; //saving x positions into array with a unique identifier
var P1y = P1positions[i][1]; //saving y positions into array with a unique identifier
if (distanceBetween(P1x, P1y, Player1.x, Player1.y) < 15) { //checking distance between Player 1 and its trail
P1speed = 0;
}
}
Player1.x += P1speed * Math.cos(P1Dir); //this makes player 1 move forard
Player1.y += P1speed * Math.sin(P1Dir); //this makes player 2 move forward
var P1trail:Shape = new Shape; //defining a variable for player 1's trail
graphics.lineStyle(8, player1col); //setting the format for the trail
graphics.drawCircle(Player1.x, Player1.y, 1.4); //drawing the circles within the trail
addChild(P1trail); //adding the circles to the stage
}
function P1fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
P1leftPressed = true; //tells the computer that left has been pressed
break;
}
case Keyboard.RIGHT:
{
P1rightPressed = true; //tells the computer that right has been pressed
break;
}
}
}
function P1fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
P1leftPressed = false; //tells the computer that left has been released
break;
}
case Keyboard.RIGHT:
{
P1rightPressed = false; //tells the computer that left has been released
break;
}
}
}
function distanceBetween (x1:Number, y1:Number, x2:Number, y2:Number) { // creating a function
// return d = Math.sqrt(x2 - x1)^2 +(y2 - y1)^2);
var diffX = x2 - x1; // creating variable to tidy up the pythagoras line below
var diffY = y2 - y1; // creating variable to tidy up the pythagoras line below
return Math.sqrt(diffX * diffX + diffY * diffY); // using pythagras theorem
}
// Player 2
var player2col = "0x0066CC";
var Player2:Shape = new Shape(); //defining a variable for Player 1
Player2.graphics.lineStyle(10,0xffff00); //defining the colour of the style
Player2.graphics.beginFill(0xffff00); //begin filling the shape
Player2.graphics.drawRoundRect(0,0,3,3,360);
Player2.graphics.drawCircle(Player2.x, Player2.x, 2.4) //draw a circle
Player2.graphics.endFill(); //finish the filling process
addChild(Player2); //add player 1 to stage
Player2.x = 500;
Player2.y = 500;
var P2leftPressed:Boolean = false;
var P2rightPressed:Boolean = false;
var P2speed = 3.5;
var P2Dir = 180;
Player2.addEventListener(Event.ENTER_FRAME, P2fl_MoveInP1DirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, P2fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, P2fl_UnsetKeyPressed);
function P2fl_MoveInP1DirectionOfKey(event:Event)
{
if(Player2.hitTestObject(leftBorder) || Player2.hitTestObject(rightBorder) || Player2.hitTestObject(topBorder) || Player2.hitTestObject(bottomBorder)){
P2speed = 0;
Player2.removeEventListener(Event.ENTER_FRAME, P2fl_MoveInP1DirectionOfKey);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, P2fl_SetKeyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, P2fl_UnsetKeyPressed);
}
if (P2leftPressed)
{
P2Dir -= 0.1;
}
if (P2rightPressed)
{
P2Dir += 0.1;
}
P2position = [Player2.x, Player2.y];
//trace(P2position);
P1positions.push(P2position);
for (var a = 0; a < P1positions.length - 10; a++) {
var P2x = P1positions[a][0];
var P2y = P1positions[a][1];
if (distanceBetween(P2x, P2y, Player2.x, Player2.y) < 15) {
P2speed = 0;
}
}
Player2.x += P2speed * Math.cos(P2Dir);
Player2.y += P2speed * Math.sin(P2Dir);
var P2trail:Shape = new Shape;
graphics.lineStyle(8, player2col);
graphics.drawCircle(Player2.x, Player2.y, 1.4);
addChild(P2trail);
}
function P2fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case event.keyCode = 90:
{
P2leftPressed = true;
break;
}
case event.keyCode = 67:
{
P2rightPressed = true;
break;
}
}
}
function P2fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case event.keyCode=90:
{
P2leftPressed = false;
break;
}
case event.keyCode=67:
{
P2rightPressed = false;
break;
}
}
}
Please help!

Attempt at title... Spawning movie clips according to X/Y axis of mouse AND changing each MC copies' frame position

This is hard to explain.
My aim is to spawn sheep(symbol) each time i click placed according to the mouse's X/Y axis... . I want it to spawn copies of the sheep BUT i also want each copy to be a different image.. (each time the mouse is clicked, it will change the image inside the sheep's symbol (placed on different frames).
If anyone can help I will appreciate it so much!
This is the code:
stage.addEventListener (MouseEvent.CLICK, makeABox);
var i:Number = 1;
function makeABox(e:Event):void {
var newSheep:myMC = new myMC();
addChild(newSheep);
newSheep.x = stage.mouseX;
newSheep.y = stage.mouseY;
i++;
}
var numPressed:Number = 0;
stage.addEventListener(MouseEvent.CLICK, countUp);
function countUp(evt:MouseEvent):void {
numPressed++;
if (numPressed == 1) {
sheep.gotoAndPlay(1);
}
else if (numPressed == 2) {
sheep.gotoAndPlay(2);
}
else if (numPressed == 3) {
sheep.gotoAndPlay(3);
}
else if (numPressed == 4) {
sheep.gotoAndPlay(4);
}
else if (numPressed == 5) {
sheep.gotoAndPlay(5);
}
else if (numPressed == 6) {
sheep.gotoAndPlay(6);
}
else if (numPressed == 7) {
sheep.gotoAndPlay(7);
}
if(numPressed >= 7) numPressed = 0;
}
Replace all the code you posted in your question with this:
stage.addEventListener (MouseEvent.CLICK, addSheep);
var sheepCounter:int = 1;
function addSheep(event:MouseEvent):void {
//create new sheep
var newSheep:myMC = new myMC();
//position it at mouse click
newSheep.x = stage.mouseX;
newSheep.y = stage.mouseY;
//set frame of sheep to display
newSheep.gotoAndStop(sheepCounter);
//add sheep to display list
addChild(newSheep);
//update counter
sheepCounter++;
if (sheepCounter==8)
{
sheepCounter = 1;
}
}

Is it possible to get the label of the next frame in flash?

Had a look for this but nothing seemed clear at the moment I have a script which will only play if the frame is the currentFrameLabel or rewind.
However in order for it not to go one frame too far I need to be able to stop it on the frame before the change not on the change.
Or am I just going about this the wrong way?
For example:
Frame 10 Label: Up
Frame 12-36 Label: Idle Loop
Frame 37 Label: Hand Up
I need it to only play from frames 12 to 36 but at the moment it plays from frames 12-37.
var reverse:Boolean = false;
var robotlabel:String = 'Up/Down';
what.addEventListener(MouseEvent.MOUSE_OVER, botAction);
what.addEventListener(MouseEvent.MOUSE_OUT, botAction2);
function botAction(evt:MouseEvent):void{
reverse = false;
robotlabel = 'Hand up/Down';
robot.gotoAndPlay('Hand up/Down');
robot.addEventListener(Event.ENTER_FRAME,run);
}
function botAction2(evt:MouseEvent):void{
reverse = true;
robot.prevFrame();
}
function run(e:Event):void{
trace("label:" + robotlabel);
trace("current" + robot.currentFrameLabel);
if(robot.currentFrameLabel != robotlabel && robot.currentFrameLabel != null){
trace("stoooooppppp");
robot.stop();
}
if(reverse == true && currentFrameLabel==robotlabel){
robot.prevFrame();
trace("reversing!");
}else if(reverse == false && (currentFrameLabel==robotlabel || robot.currentFrameLabel == null)){
robot.nextFrame();
}else{
trace("destroy");
reverse = false;
robot.stop();
robot.removeEventListener(Event.ENTER_FRAME,run);
}
}
There is not a "nextFrameLabel" property as such in as3, however you can get an array of all the frame labels and numbers in your target movieclip using the currentLabel property of the MovieClip and work it out from there, since you know the currentFrame at all times.
Quick example from the docs:
import flash.display.FrameLabel;
var labels:Array = mc1.currentLabels;
for (var i:uint = 0; i < labels.length; i++)
{
var label:FrameLabel = labels[i];
trace("frame " + label.frame + ": " + label.name);
}
In case this is useful to anybody else I solved it by looping through each frame and then storing the corresponding label to the frame so it can later be checked against.
for (i = 1; i < robot.totalFrames; i++)
{
for (var n:uint = 0; n < this.robolabels.length; n++)
{
if(this.robolabels[n].frame == i){
newLabel = this.robolabels[n].name
}
}
this.roboframes[i] = newLabel;
}

How to make object reset my game?

I am a student working on a project with flash. I am simply trying to make a flash game using Actionscript 3 in Adobe Flash CS5. Everything is working fine, i can walk and jump, but I want the game to restart if the Main character touches an object and dies (like spikes in a pit). I have a main menu in frame 1 and the first level on frame 2. The main character is called "player_mc" and the object that kills him is called "dies". Help and tips is very much appreciated.
Code:
//The Code For The Character (Player_mc)
import flash.events.KeyboardEvent;
import flash.events.Event;
var KeyThatIsPressed:uint;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var playerSpeed:Number = 20;
var gravity:Number = 1;
var yVelocity:Number = 0;
var canJump:Boolean = false;
var canDoubleJump:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
//This two functions cllapsed is the keybindings that the game uses
function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.D)
{
rightKeyIsDown = true;
}
if(event.keyCode == Keyboard.A)
{
leftKeyIsDown = true;
}
if(event.keyCode == Keyboard.SPACE)
{
upKeyIsDown = true;
}
if(event.keyCode == Keyboard.S)
{
downKeyIsDown = true;
}
}
function ReleaseAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.D)
{
rightKeyIsDown = false;
}
if(event.keyCode == Keyboard.A)
{
leftKeyIsDown = false;
}
if(event.keyCode == Keyboard.SPACE)
{
upKeyIsDown = false;
}
if(event.keyCode == Keyboard.S)
{
downKeyIsDown = false;
}
}
//Player animations
player_mc.addEventListener(Event.ENTER_FRAME, moveThePlayer);
function moveThePlayer(event:Event):void
{
if(!canJump || rightKeyIsDown && !canJump || leftKeyIsDown && !canJump)
{
player_mc.gotoAndStop(3);
}
if(!upKeyIsDown && !rightKeyIsDown && !leftKeyIsDown && canJump)
{
player_mc.gotoAndStop(1);
}
if(rightKeyIsDown && canJump || leftKeyIsDown && canJump)
{
player_mc.gotoAndStop(2);
}
//Animation stops here
if(rightKeyIsDown)
{
floor_mc.x -= playerSpeed;
dies.x -= playerSpeed;
player_mc.scaleX = 0.3;
}
if(leftKeyIsDown)
{
floor_mc.x += playerSpeed;
dies.x += playerSpeed;
player_mc.scaleX = -0.3;
}
if(upKeyIsDown && canJump)
{
yVelocity = -18;
canJump = false;
canDoubleJump = true;
}
if(upKeyIsDown && canDoubleJump && yVelocity > -2)
{
yVelocity =-13;
canDoubleJump = false;
}
if(!rightKeyIsDown && !leftKeyIsDown && !upKeyIsDown)
{
player_mc.gotoAndStop(1);
}
yVelocity +=gravity
if(! floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y+=yVelocity;
}
if(yVelocity > 20)
{
yVelocity =20;
}
for (var i:int = 0; i<10; i++)
{
if(floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y--;
yVelocity = 0;
canJump = true;
}
}
}
If I understand this right, when you die, you want to restart the level. To do this, you could save all your initial variables like player position, floor position, etc in an array, then on death, restore those variables to their corresponding object. However, when you get more complex and have lots and lots of objects, it could become tricky.
A simpler solution is to display a death screen of some sorts on another frame (lets call it frame 3) when you hit the dies object, and on that death screen when you press a restart button or after a certain time delay, it then goes back to frame 2 and restarts the level.
Example:
Put this in your moveThePlayer function:
if(dies.hitTestPoint(player_mc.x, player_mc.y, true))
{
// remove all the event listeners
player_mc.removeEventListener(Event.ENTER_FRAME, moveThePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
gotoAndStop(3); // this has your "dead" screen on it.
}
And on frame 3, have something like:
import flash.events.MouseEvent;
retry_button.addEventListener(MouseEvent.CLICK, retry);
function retry(e:MouseEvent) {
retry_button.removeEventListener(MouseEvent.CLICK, retry);
// back to the level
gotoAndStop(2);
}
You could also do something like storing a variable on which frame you want to go back to, so you have a single retry frame which can go to whatever level you were just on.
A good habit that you should develop is having an endGame() or similar method that you update as you work on the game.
For example:
function endGame():void
{
// nothing here yet
}
Now lets say you create a player. The player is initially set up like this:
player.health = 100;
player.dead = false;
You'll want to update your endGame() method to reflect this.
function endGame():void
{
player.health = 100;
player.dead = false;
}
Got some variables related to the game engine itself?
var score:int = 0;
var lives:int = 3;
Add those too:
function endGame():void
{
player.health = 100;
player.dead = false;
score = 0;
lives = 3;
}
Now endGame() should reset the game back to its original state. It's much easier to work this way (as you code the game) rather than leaving it until last and trying to make sure you cover everything off, which is unlikely.