This document contains code for a 2D game. It defines variables and functions for player movement and collisions with the ground. There are also functions for enemy behavior, updating variables, and handling collisions between objects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
55 views3 pages
Thingy
This document contains code for a 2D game. It defines variables and functions for player movement and collisions with the ground. There are also functions for enemy behavior, updating variables, and handling collisions between objects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3
stop(); //stop from going to next frame.
var g=0.4; //Variable for gravity
var f=0.8; //Variable for friction //We use player before each variable below, to assign the variables to the playe r object. player._x=Stage.width/2; //Players, x (horizontal location). player.vx=0; //Velocity X player.vy=0; //Velocity y player.power=1; //Power used to increase the vx for moving. player.jump=-10; //What vy will be set to when jumping. player.jumping=false; //If the player is jumping player.health=50; //The players health //Below we use enemy before the variables to assign the variables to the enemy o bject. enemy.vx=0; //Enemys vx enemy.vy=0; //Enemys vy enemy.power=0.5; //Enemys power enemy.jump=-8; // Enemys jump enemy.jumping=false; //if the enemy is jumping this.onEnterFrame=function(){ //On every frame run this code brain(enemy,player); //Run the brain function with the "enemy" as the ob val ue, and "player" as the tr value. player.vx*=f; //Multiply the playres vx value by f which will decrease it sl owing the player down. player.vy+=g; //Add gravity to the players vy value. player._y+=player.vy; //Move the players x position based off its vy. healthbar.gotoAndStop(player.health); //Set the healthbars frame to player.h eath if(player.health<=0){ //if the players heath goes below 0 ie Dead. cleanup(); //Run our cleanup function }// Close the if statement if(player._x>(Stage.width/2)-20 && player._x<(Stage.width/2)+20){ //if the players x is within 40 pixels of stage center. ground._x-=player.vx; //move the ground instead of the player in the opp osite direction. enemy._x-=player.vx; //move the enemy away also }else{ //else meaning if the above condition is not true then do the followi ng player._x+=player.vx; //move the player because they are out of the cent er }//close the else. updater(enemy); //run the update function using "enemy" as the ob value. if(Key.isDown(Key.LEFT)&& !_root.ground.hitTest(player._x-(player._width/2)- 3,player._y,true)){ //If the left arrow key is down and the player is not hitting the ground to the left player.vx-=player.power; //decrease the players vx by its power }else if(Key.isDown(Key.RIGHT)&& !_root.ground.hitTest(player._x+(player._wi dth/2)+3,player._y,true)){ //if the above is not true and right arrow key is down and the player is not hitting the ground to the right of it. player.vx+=player.power; //Increase the players vx by its power }//close the else if if(Key.isDown(Key.UP)&& !player.jumping){ //if the up arrow key is down and the player is not jumping player.vy+=player.jump; //add the value of jump to players vy player._y+=player.jump; //move the players y to prevent it being caught by the collisions. player.jumping=true; //The player is now jumping }//close the if collisions(player); //run the collisions function with "player" as the value of ob collisions(enemy); // run the collisions function with "enemy" as the value of ob }//close the function //On Enter Frame Event function updater(ob){ //creates a function called updater with one input variabl e ob.vx*=f; //uses the ob values defined when calling the function to multiply its vx by the friction variable ob.vy+=g; //add gravity ob._x+=ob.vx; //update the objects x ob._y+=ob.vy; //and its y }//close the function //updates player and enemy vars function collisions(ob){ //Create a function called collisions with one input va riable. while(_root.ground.hitTest(ob._x,ob._y-(ob._height/2),true)){ //while the objects top is inside the ground it will run the following ob._y+=0.1; //moves its y position down ob.vy=0; //and set the objects vy to 0 so if it is jumping it will not c ontinue going up. }//end the while loop while(_root.ground.hitTest(ob._x+(ob._width)/3,ob._y+(ob._height/2),true) || _root.ground.hitTest(ob._x-(ob._width)/3,ob._y+(ob._height/2),true)){ //while the objects lower left or lower right is inside the ground ob._y-=0.1; //Move the objects y position up ob.vy=0; //set its vy to 0 so if it is falling it will stop falling ob.jumping=false; //the player is no longer jumping }//close the while while(_root.ground.hitTest(ob._x+(ob._width/2),ob._y-(ob._height/2),true) || _root.ground.hitTest(ob._x+(ob._width/2),ob._y+(ob._height/3),true)){ //while the objects upper right or lower right is in the ground then ob._x-=0.1; //move the object left ob.vx=0; //set its vx to 0 to stop it from moving }//close the while while(_root.ground.hitTest(ob._x-(ob._width/2),ob._y-(ob._height/2),true) || _root.ground.hitTest(ob._x-(ob._width/2),ob._y+(ob._height)/3,true)){ //while the objects upper left or lower left is in the ground then ob._x+=0.1; //move the object left ob.vx=0; //set its vx to 0 }//close the while }//close the function //Checks and handles collisions function brain(ob,tr){ //Create a function called brain with two input variables ob for object and tr for target if(ob._x<tr._x-10){ //if the object(enemy) is less then the targets x-10 the n ob.vx+=ob.power; //increase its vx to move it right }//close the if else if(ob._x>tr._x+10){//if the above is not true and the object is greater then the target x+10 then ob.vx-=ob.power;//decrease its vx to move it left }else if(ob._y>tr._y-(tr._height/2) && ob._y<tr._y+(tr._height/2)){ //if non of the above are true and if the target is within the y boundaries then tr.health--;//the object is close enough to the target to decrease its h ealth }//close the else if(ob._y>tr._y && !ob.jumping && (_root.ground.hitTest(ob._x+(ob._width/2)+3 ,ob._y,true)|| _root.ground.hitTest(ob._x-(ob._width/2)-3,ob._y,true))){ //if the target is higher then the object and the object is not jumping and the object is against the ground ob.vy=ob.jump; //set the objects vy to the objects jump ob._y+=ob.jump; //move the object up. ob.jumping=true; //the object is now jumping }//close the if }//close the function //Enemys brain function cleanup(){ //create a function called function delete this.onEnterFrame; //delete the onEnterFrame event so it does not con tinue running gotoAndStop(2);//move the root timeline to frame 2 }//close the function //Cleans up everything and moves to the loose frame