Moving Objects
Objectives You will know … the Stage default dimensions are 550 x 400 px The Flash coordinate system How to write a function to move objects Left, right, up, down, and diagonally How to screen wrap a moving object so it repeats its movement How to set objects at random locations Initially When screen wrapping How to run your function using an add event listener How to turn off your function a remove event listener
Flash Coordinate System.x is used to move objects from side to side.y is used to move objects up or down
Flash Coordinate System Stage is 5550 px wide and 400 px high (default) An object moving right has an increasing x coordinate The Stage’s x-axis goes from 0 to 550
Flash Coordinate System Stage is 5550 px wide and 400 px high (default) An object moving left has an decreasing x coordinate The Stage’s x-axis goes from 0 to 550
Flash Coordinate System Stage is 5550 px wide and 400 px high (default) An object moving down has an increasing y coordinate y-axis goes from 0 to 400
Flash Coordinate System Stage is 5550 px wide and 400 px high (default) An object moving up has an decreasing y coordinate y-axis goes from 0 to 400
Flash Coordinate System Stage is 5550 px wide and 400 px high (default) An object moving diagonally has a change to both the x and the y coordinates (increasing and/or decreasing)
This function moves three objects down the Stage Write a Function function moveRocks (e:Event) { rock1.y = rock1.y + 20; //falling rocks rock2.y = rock2.y + 25; rock3.y = rock3.y + 15; } Made up name Varying speeds 3 rocks Notice it is Event, not MouseEvent
Use a combination operator to write shorter statements cloud.x += 10; This makes an object with instance name ‘cloud’ move right 10 pixels at a time The longer way to write the same instruction is cloud.x = cloud.x + 10; Combination Operators
Updating Any Movie Clip Property Continuously (Examples) Rotate Clockwise hand.rotation += 10; Rotate Counter-Clockwise wheel.rotation -= 10; Increase in Size shape.width +=10; shape.height += 10; Move to the Right car.x += 5; Move to the Left car.x -= 5; Decrease in Size star.width -= 10; star.height -= 10; Move Up bullet.y -= 7; Move Down bullet.y += 7; Increase Proportionately shape.scaleX +=.05; shape.scaleY +=.05; Fade Out ghost.alpha -=.05; Fade In ghost.alpha +=.05; Decrease Proportionately star.scaleX -=.05; star.scaleY -=.05;
Often we want to repeat our movements when an object leaves the Stage so we will add if statements to screen wrap Screen Wrap function moveCloud(e:Event) { cloud.x = cloud.x - 10; //moves left if(cloud.x < 0) { cloud.x = 560; //re-set stage right }
When your game starts, usually we want our moving objects to begin at random locations Add code to the main Script pane (not in a function) to set a random start position for each object Random Locations cloud.y = Math.random() * 400; Multiply by 400 so the cloud can randomly be placed on the y-axis anywhere from 0 up to 400
As the moving object is screen wrapped, reset it to a new random location Random Locations function moveCloud(e:Event) { cloud.x = cloud.x - 10; //moves left if(cloud.x < 0) { cloud.y = Math.random() * 400; cloud.x = 560; //re-set stage right } random y location
To run your function you can add an event listener which calls each function You will use the ENTER_FRAME event to make a continuous change Add Your Event Listeners addEventListener(Event.ENTER_FRAME, moveRocks);
Before you leave a page (to win or lose, for example) you MUST remove all event Listeners that have an ENTER_FRAME event type Example: You win the game if your score reaches 20 Remove Your Event Listeners! if score == 20) { removeEventListener(Event.ENTER_FRAME, moveStuff); gotoAndStop(3); //win page }