Hi guys totally new with Flash, don’t understand coding AT ALL. Unfortunately at uni I have a module in it where I have to create a piece of animation! Mines is a moonwalking penguin with an arctic background. The background is wider than the stage and to make the penguin moonwalk right or left it’s actually the background that moves. My problem is that the background once you go so far left or right it goes into white space. The guy helping me made it in coding so that copies of the background spawn and should stop that from happening once the ‘x’ position gets to a certain point as a new background spawns at that edge. It doesn’t work and I dunno how to fix it. Also I want the music to only start when you press left or right on the arrow keys i.e. only when the penguin is ‘moving’ Here’s my code so far, someone pleaase help!!!
import flash.events.KeyboardEvent;
import flash.events.Event;
//Variables
//creates an array ‘keysArray’ that stores 4 Boolean values which all start false
var keysArray : Array = new Array(false, false);
// left, right
//new variable
var old_Background:MovieClip;
var new_Background:MovieClip = null;
old_Background = backg;
var leftBumping:Boolean = false
var rightBumping:Boolean = false
var leftBumpPoint
//Check key states
//adds an EventListener that is called when a key is pressed down
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
//adds an EventListener that is called when a key is released
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
var moveSpeed : int=5; //creates a variable ‘moveSpeed’ that has the value of 5
//new function ‘keyPressed’ which handles a KeyboardEvent which we’ll refer to //as keyEvent and which returns no value
var listOfHoles : Array = new Array(); //creates a new empty array called ‘listOfHoles’
function keyPressed( keyEvent : KeyboardEvent ) : void
{
if (keyEvent.keyCode==37) //if the key pressed is ‘keyCode’ 37 i.e LEFT
{
trace(“Left Arrow pressed.”);
keysArray[0]=true;
}
if(keyEvent.keyCode==39) //if the key pressed is ‘keyCode’ 39 i.e RIGHT
{
trace(“Right Arrow pressed”);
keysArray[1]=true;
}
}
//new function ‘keyReleased’ which handles a KeyboardEvent which we’ll refer to //as keyEvent and which returns to no value
function keyReleased( keyEvent : KeyboardEvent ) : void
{
if(keyEvent.keyCode==37) //if key released is ‘keyCode’ 37 i.e LEFT
{
trace(“Left Arrow released.”);
keysArray[0]=false;
}
if(keyEvent.keyCode==39) //if key released is ‘keyCode’ 39 i.e RIGHT
{
trace(“Right Arrow released.”);
keysArray[1]=false
}
}
//Update the characters position
//add an EventListener that listens for a new frame being entered and which calls//the update function
stage.addEventListener(Event.ENTER_FRAME, update);
//new function ‘update’ that handles and ‘Event’ labelled ‘event’ which returns no value
function update( event : Event ) : void
{
trace("bkg width: " + old_Background.width);
//trace("new spawn pt right: " + -(old_Background.width - stage.stageWidth).toString());
if( keysArray[0]==true ) //if the boolean associated with the LEFT key is true
{
wholepenguin.play();
wholepenguin.scaleX=-0.8;
old_Background.x+=moveSpeed; //subtract the moveSpeed from the X position
if(new_Background != null)
{
new_Background.x+=moveSpeed;
}
}
if( keysArray[1]==true ) //if the boolean associated with the RIGHT key is true
{
wholepenguin.play();
wholepenguin.scaleX=0.8;
old_Background.x-=moveSpeed; //add the moveSpeed to the X position
if(new_Background != null)
{
new_Background.x-=moveSpeed;
}
}
if( keysArray[0]==false && keysArray[1]==false )
{
wholepenguin.stop();
}
trace("oldbkg.x: " + old_Background.x);
if(old_Background.x >= 0)
{
trace(“oldbkg >= 0”);
MakeNewBackground();
}
if(old_Background.x <= -old_Background.width+stage.stageWidth)
{
trace(“oldbkg <= -old_Background.width-stage.stageWidth (” + (-old_Background.width+stage.stageWidth).toString() + “)”);
MakeNewBackground();
}
}
function MakeNewBackground ()
{
if(new_Background == null)
{
trace(“making new bkg”);
new_Background = new Background ();
if(old_Background.x >= 0)
{
new_Background.x = old_Background.x - new_Background.width;
}
if(old_Background.x < 0)
{
new_Background.x = old_Background.width + old_Background.x;
trace("spawned at: " +(old_Background.width + old_Background.x).toString());
}
new_Background.x = old_Background.x - new_Background.width;
new_Background.y = 0;
stage.addChildAt(new_Background, 0);
stage.setChildIndex(wholepenguin, stage.numChildren - 1);
stage.addChild(new_Background);
setChildIndex(wholepenguin, numChildren - 1);
}
}