Followers

Friday, April 24, 2015

Flash: Buttons and Scenes Code

Below is code to make your buttons work with scenes.
To read more about it go here
http://www.myglife.org/usa/eaprepmlk/index.php/Help:Ingredients_-_Buttons


Draw 4 buttons on Scene 1.
Each button should have an instant name of nextButton1, nextButton2, and so on.
Make 4 scenes.
Copy the buttons to all of your scenes.
Now copy the code and put it on Scnene 1.
Test.

Here is the code...


stop();
 
import flash.events.MouseEvent;
import flash.ui.Mouse;
 
addEventListener(MouseEvent.CLICK, onMouseClick);
 
function onMouseClick(evt:MouseEvent)
{
 if(evt.target.name == "nextButton1")
 {
  gotoAndStop(1, "Scene 1");
 }
        if(evt.target.name == "nextButton2")
 {
  gotoAndStop(1, "Scene 2");
 }
      if(evt.target.name == "nextButton3")
 {
  gotoAndStop(1, "Scene 3");
 }
    if(evt.target.name == "nextButton4")
 {
  gotoAndStop(1, "Scene 4");
 }
}

Thursday, April 16, 2015

Flash: Mobile Mouse Control Code

This code uses Mouse Control for Player.
The enemy and collectable object will slowly scroll across the screen.




import flash.events.Event;

stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addChild(player);
player.x = stage.mouseX;
player.y = stage.mouseY;
var score = 0;
var health = 10;

function gameLoop(evt:Event)
{
player.x  = stage.mouseX;
player.y  = stage.mouseY;

enemy.x = enemy.x - 1;
collectable.x = collectable.x - 3;


// IF THE PLAYER HITS THE COLLECTABLE

if(player.hitTestObject(collectable) && collectable.visible == true)

{
collectable.visible = false;
score = score + 1;
}

scoreDisplay.text = "Score: " + score;

//IF THE PLAYER HITS THE ENEMY

if(player.hitTestObject(enemy) && enemy.visible == true)

{
enemy.visible = false;
health = health - 1;
}

healthDisplay.text = "Health: " + health;
}

Wednesday, April 8, 2015

Flash: Enemy Chase Code

import flash.events.Event;

stage.addEventListener(Event.ENTER_FRAME, gameLoop);

function gameLoop(evt:Event)
{
 if(enemy.x < player.x)
{
     enemy.x +=1;
}
if(enemy.x > player.x)
{
     enemy.x -=1;
}
if(enemy.y < player.y)
{
     enemy.y +=1;
}
if(enemy.y > player.y)
{
     enemy.y -=1;
}
}

Flash: Mouse Control of the Player

player.x = stage.mouseX;
player.y = stage.mouseY;

Tuesday, April 7, 2015

Flash: AS3 Player Control with Arrow Keys

Originally published 2/5/15


Copy the code below and add it to your actions in Flash.


import flash.events.Event;
import flash.events.KeyboardEvent;
 
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
addEventListener(Event.ENTER_FRAME, gameLoop);
 
var playerSpeed = 2;
player.x = 400;
 
var isLeftKeyDown = false;
var isRightKeyDown = false;
var isUpKeyDown = false;
var isDownKeyDown = false;
 
function onKeyPressed(evt:KeyboardEvent) {
 
 if(evt.keyCode == Keyboard.LEFT) {
     isLeftKeyDown = true;
 }
 
 if(evt.keyCode == Keyboard.RIGHT) {
     isRightKeyDown = true;
 }
 
 if(evt.keyCode == Keyboard.UP) {
     isUpKeyDown = true;
 }
 
 if(evt.keyCode == Keyboard.DOWN) {
     isDownKeyDown = true;
 }
}
 
function onKeyReleased(evt:KeyboardEvent) {
 
 if(evt.keyCode == Keyboard.LEFT) {
     isLeftKeyDown = false;
 }
 
 if(evt.keyCode == Keyboard.RIGHT) {
     isRightKeyDown = false;
 }
 
 if(evt.keyCode == Keyboard.UP) {
     isUpKeyDown = false;
 }
 
 if(evt.keyCode == Keyboard.DOWN) {
     isDownKeyDown = false;
 }
}
 
function gameLoop(evt:Event) {
 if(isLeftKeyDown == true) {
     player.x-=playerSpeed;
 }
 
 if(isRightKeyDown == true) {
     player.x+=playerSpeed;
 }
 
 if(isUpKeyDown == true) {
     player.y-=playerSpeed;
 }
 
 if(isDownKeyDown == true) {
     player.y+=playerSpeed;
 }
}