Followers

Showing posts with label AS3. Show all posts
Showing posts with label AS3. Show all posts

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;
 }
}

Monday, March 30, 2015

Flash Mobile: Enemy Scroll and Collect Collectable Code

Below is Actionscript 3 code that once is input into your Actions Frame in Flash will allow your player character to move with the mouse, collect an object and the enemy will scroll from right to left.

(Originally published 2/11/15)


import flash.events.Event;
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addChild(player);

var score = 0;
var health = 0;

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

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

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

        if(player.hitTestObject(enemy) && enemy.visible == true)
        {
     health = health - 5;
              enemy.visible = false;
        }

if(player.hitTestObject(collectable) && collectable.visible == true)
        {
     score = score +1;
              collectable.visible = false;
        }

        scoreDisplay.text = "Score:" + score;
        healthDisplay.text = "Health:" + health;

}

Thursday, February 5, 2015

Flash: Mobile Collectable Object Code

d. Scroll Your Collectable Object
  • Select your Actions layer and open the Actions window
  • In the gameLoop() function, add the following code to scroll your collectable to the left:
collectable.x = collectable.x -3;

Monday, February 2, 2015

Flash: AS3 Health Bar Code

import flash.events.Event;

stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addChild(player);

var health = 100;

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

function gameLoop(evt:Event)
{
if(player.hitTestObject(enemy) == true) {
       health = health - 1;
}

healthBar.scaleX = health / 100;

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

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;
}
}

Thursday, January 29, 2015

Flash Enemy Chase Code AS3

import flash.events.Event;

stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addChild(player);

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

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


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;
}
}