I am extremely new to actionscript and have begun working on a code for a class that causes background elements to scroll and loop when you push a button. Here is the code as I have currently written it;
ActionScript Code:
// add elements to the stage
// create variables
var hills1:BgroundHills = new BgroundHills();
var hills2:BgroundHills = new BgroundHills();
var grass1:BgroundGrass = new BgroundGrass();
var grass2:BgroundGrass = new BgroundGrass();
// add children
addChild(hills1);
addChild(hills2);
addChild(grass1);
addChild(grass2);
// set default positions
hills1.x = 0;
hills2.x = hills1.width;
grass1.x = 0;
grass2.x = grass1.width;
// variable to control scroll speed
var scrollSpeed:Number = 0;
// variable to alternate scrolling on/off
var scrolling:Boolean = false;
// listen to see if scrolling and update scroll speed accordingly
stage.addEventListener(Event.ENTER_FRAME, checkScrolling);
function checkScrolling(event:Event):void {
if (scrolling = true) {
scrollSpeed = 2;
} else {
scrollSpeed = 0;
}
}
// listen to see if element positions need updating
stage.addEventListener(Event.ENTER_FRAME, updatePosition);
function updatePosition(event:Event):void {
hills1.x -= scrollSpeed;
hills2.x -= scrollSpeed;
grass1.x -= scrollSpeed;
grass2.x -= scrollSpeed;
}
When I test the current script, even though scrolling is set to false by default, the elements hills1, hills2, grass1, and grass2 begin to scroll. Upon tracing the scrollSpeed it appears it gets set to 2 almost the moment it compiles.
I am unsure as to where I have gone wrong here as the code appears perfectly fine to me up until this point and I have received no compile errors.
If anyone could offer help it would be greatly appreciated.