PDA

View Full Version : [AS3] Moving background


rensk
04-01-2010, 12:52 PM
Hello i'm making a catching game and i got objects falling down from the top of te screen but the background also moves and the background needs to stay in place.
my character does stay in place.

can someone please take a look at this

information_txt.text = "Gebruik de pijltjes toetsen om te bewegen!";

var urls:Array = ["gadget1small.png", "gadget2small.png"];
var loaders:Array;
var numComplete:uint;
var gadgets:Array;
var count:uint = 0;

var rightArrow:Boolean;
var leftArrow:Boolean;
var speed:int = 3.5;

var stage_width:int = stage.stageWidth - character.width;

var stage_height:int = stage.stageHeight - character.height;

loadFiles(urls);
function loadFiles(files:Array = null):void {
if (files == null || files.length <= 0) {
return;
}
loaders = [];
numComplete = 0;
for (var i:int = 0; i < files.length; i++) {
loadFile(files[i]);
}
}

function loadFile(url:String):void {
if (url == null || url == "") {
return;
}
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, fileLoadCompleteHandler);
loader.load(new URLRequest(url));
loaders.push(loader);
}

function fileLoadCompleteHandler(e:Event):void {
numComplete++;
if (numComplete == loaders.length) {
gadgets = [];
for (var i:int = 0; i < loaders.length; i++) {
var loader:Loader = loaders[i]
var tempBitmap:Bitmap = loader.content as Bitmap;
gadgets.push(tempBitmap.bitmapData);
}
initialize();
}
}

function initialize():void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);
}

function createGadget():void {
var bpd:BitmapData = gadgets[int(Math.random() * 3)];
var bp:Bitmap = new Bitmap(bpd);
addChild(bp);
bp.x = Math.random() * stage_width;
}

function keyPressed(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = true;
}
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}

}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}

}


function everyFrame(event:Event):void {
count++
if (count > 9) {
count = 0;
createGadget();
}

if (rightArrow && character.x < stage_width) {
character.x += speed;
}
if (leftArrow && character.x > 0) {
character.x -= speed;

}
if (numChildren > 0)
for (var i:int = 0; i < numChildren; i++) {
if (getChildAt(i) != character && getChildAt(i) != information_txt)
{
if (getChildAt(i).y < stage_height)
getChildAt(i).y += speed
else
removeChild(getChildAt(i));
}
}
}

rensk
04-02-2010, 09:13 AM
Does someone know what is wrong with my code ?

rensk
04-02-2010, 01:21 PM
Here is a link with the .Fla and images to make it easier

http://www.datafilehost.com/download-aa00971c.html

TomMalufe
04-02-2010, 01:49 PM
I haven't really looks at much here, but I would guess your problem is here.
if (numChildren > 0)
for (var i:int = 0; i < numChildren; i++) {
if (getChildAt(i) != character && getChildAt(i) != information_txt)
{
if (getChildAt(i).y < stage_height)
getChildAt(i).y += speed
else
removeChild(getChildAt(i));
}
}


I would suggest instead of doing that you store all the items you want to fall in an array and just loop through that.

For example:
var gadgets:Array = [];

function createGadget():void {
var bpd:BitmapData = gadgets[int(Math.random() * 3)];
var bp:Bitmap = new Bitmap(bpd);
addChild(bp);
gadgets.push(bp);
bp.x = Math.random() * stage_width;
}


/** In the ENTER_FRAME handler function **/

//you don't need the if(gadgets.length > 0) because
// i is 0 already and that is checked in this for loop
for (var i:int = 0; i < gadgets.length; i++)
{
if (gadgets[i].y < stage_height)
{
gadgets[i].y += speed;
}
else
{
//This removes the gadget from both the display list and the array.
removeChild(gadgets.splice(i,1)[0]);
}
}


Give it a shot and see if it works. I wrote this code here in this post and not in an editor so I can't be sure that it doesn't have any typos or anything, so try getting the idea from what I wrote here and then do it yourself.

rensk
04-02-2010, 02:19 PM
Thnx for the help but i have no idea what too replace with what and what i can throw away:confused: