with the following coding i managed to create a game that allowed my player to move around the stage and collect pumpkins. for every pumpkin the player collected you would have points added to your score. if the player is hit by a bat then the game will stop and the player will disappear
for my game i had to code each bat to get them to move randomly. i wanted to use a for loop to cut back on the amount of coding but it wouldn't work the way i wanted it to so i resorted to just repeating the coding for every bat i wanted up.
var baty = Math.random() * 13;
var batx = Math.random() * 13;
var speed = 1;
var mynumber:Number = 0;
trace(mynumber);
var inGame:Boolean = true;
swoop.addEventListener(Event.ENTER_FRAME , op);
function op(e:Event):void {
if (inGame==true) {
swoop.x += batx + Math.random() * 5;//makes bat move
swoop.y += baty + Math.random() * 5;
score.text = "score " + String(mynumber);
if (player.hitTestPoint(swoop.x,swoop.y,false)) {
player.alpha = .0;
inGame=false
}
}
}
swoop.addEventListener(Event.ENTER_FRAME , po);
function po(e:Event):void {
if (swoop.x >= 550) {
swoop.x = -50;
}
if (swoop.x < -50) {
swoop.x = 550;
}
if (swoop.y >= 450) {
swoop.y = 0;
}
if (swoop.y < -50) {
swoop.y = 500;
}
}
swoop2.addEventListener(Event.ENTER_FRAME , op2);
function op2(e:Event):void {
if (inGame==true) {
swoop2.x += batx + Math.random() * 5;
swoop2.y += baty + Math.random() * 5;
if (player.hitTestPoint(swoop2.x,swoop2.y,false)) {
player.alpha = .0;
inGame=false
}
}
}
swoop2.addEventListener(Event.ENTER_FRAME , po2);
function po2(e:Event):void {
if (swoop2.x >= 550) {
swoop2.x = -50;
}
if (swoop2.x < -50) {
swoop2.x = 550;
}
if (swoop2.y >= 450) {
swoop2.y = 0;
}
if (swoop2.y < -50) {
swoop2.y = 500;
}
}
swoop3.addEventListener(Event.ENTER_FRAME , op3);
function op3(e:Event):void {
if (inGame==true) {
swoop3.x += batx + Math.random() * 5;
swoop3.y += baty + Math.random() * 5;
if (player.hitTestPoint(swoop3.x,swoop3.y,false)) {
player.alpha = .0;
inGame=false
}
}
}
swoop3.addEventListener(Event.ENTER_FRAME , po3);
function po3(e:Event):void {
if (swoop3.x >= 550) {
swoop3.x = -50;
}
if (swoop3.x < -50) {
swoop3.x = 550;
}
if (swoop3.y >= 450) {
swoop2.y = 0;
}
if (swoop3.y < -50) {
swoop3.y = 500;
}
}
swoop4.addEventListener(Event.ENTER_FRAME , op4);
function op4(e:Event):void {
if (inGame==true) {
swoop4.x += batx + Math.random() * 5;
swoop4.y += baty + Math.random() * 5;
if (player.hitTestPoint(swoop4.x,swoop4.y,false)) {
player.alpha = .0;
inGame=false
//put your game over image nnnnn.visible=true
}
}
}
swoop4.addEventListener(Event.ENTER_FRAME , po4);
function po4(e:Event):void {
if (swoop4.x >= 550) {
swoop4.x = -50;
}
if (swoop4.x < -50) {
swoop4.x = 550;
}
if (swoop4.y >= 450) {
swoop4.y = 0;
}
if (swoop4.y < -50) {
swoop4.y = 500;
}
as for the movement of the player in the perspective i chose to create the game in, i had to make 4 different sides of the character. the following code tells the player's view to change depending on what key is pressed down. it also states the bounds of the stage and makes the player be able to wrap around.
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer);
// this line is telling flash to lisen for any key being presed on the keyboard;
function moveplayer(myevent:KeyboardEvent):void {
//this gives the name of the function
if (myevent.keyCode == Keyboard.RIGHT) {
player.x += 5;
player.gotoAndStop(4);
}
//if right key is hit, then rotate the player 90%;
//} ;
if (myevent.keyCode == Keyboard.LEFT) {
player.x -= 5;
player.gotoAndStop(2);
//};
//if the left key is hit then rotated 170%;
}
if (myevent.keyCode == Keyboard.UP) {
player.y -= 5;
player.gotoAndStop(3);
}
//if up key is hit then rotate nothing;
//} ;
if (myevent.keyCode == Keyboard.DOWN) {
player.y += 5;
player.gotoAndStop(1);
}
//if the down key is hit then rotate 180%
}
stage.addEventListener(Event.ENTER_FRAME, bounds);
//listen to the stage then ad event called enter frame. function is called bounds;
function bounds(event:Event) {
if (player.x >= 550) {
player.x = -50;
}
if (player.x < -50) {
player.x = 550;
}
if (player.y >= 450) {
player.y = 450;
}
if (player.y < -50) {
player.y = 500;
}
}
this coding was needed to add in multiple pumpkins in random places. it also has a hit detection in it that makes the pumpkins disappear when the player hits them
var pumArray:Array=new Array();
for (var i:int = 0; i < 15; i++) {
// for loop. will run 15 times
var Pum:MovieClip = new pumpkin();
// go to library look for box
Pum.x = Math.random() * 550;
// random position
Pum.y = Math.random() * 400;
//random position
addChild(Pum);
//add onto stage
pumArray[i] = Pum;
// fills list with 15 cubes
}
stage.addEventListener(Event.ENTER_FRAME , blam);
function blam(myevent:Event):void {
for (var i:int = 0; i < 15; i++) {
if (player.hitTestPoint(pumArray[i].x,pumArray[i].y,false)) {
pumArray[i].alpha = .0;
mynumber++;
trace(mynumber);
the following coding is used to add in the player to the stage at a random position
}
}
}
var player:MovieClip = new daman();
// go to library look for box
player.x = Math.random() * 550;
// random position
player.y = Math.random() * 400;
//random position
addChild(player);
//add onto stage
No comments:
Post a Comment