Movie Clip Properties
Download
Report
Transcript Movie Clip Properties
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
.y is used
to move
objects up
or down
.x is used to move objects from side to side
Flash Coordinate System
Stage is
5550 px
wide and
400 px
high
(default)
The Stage’s x-axis goes from 0 to 550
An object moving right has
an increasing x coordinate
Flash Coordinate System
Stage is
5550 px
wide and
400 px
high
(default)
The Stage’s x-axis goes from 0 to 550
An object moving left has an
decreasing x coordinate
Flash Coordinate System
y-axis goes from 0 to 400
Stage is
5550 px
wide and
400 px
high
(default)
An object moving down has
an increasing y coordinate
Flash Coordinate System
y-axis goes from 0 to 400
Stage is
5550 px
wide and
400 px
high
(default)
An object moving up has an
decreasing y coordinate
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)
Write a Function
This function moves three objects down the Stage
Made up name
Notice it is Event, not MouseEvent
function moveRocks (e:Event) {
3 rocks
rock1.y = rock1.y + 20; //falling rocks
rock2.y = rock2.y + 25;
rock3.y = rock3.y + 15;
}
Varying speeds
Combination Operators
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;
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;
Screen Wrap
Often we want to repeat our movements when an object leaves
the Stage so we will add if statements to screen wrap
function moveCloud(e:Event) {
cloud.x = cloud.x - 10; //moves left
if(cloud.x < 0)
{
cloud.x = 560; //re-set stage right
}
}
Random Locations
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
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
Random Locations
As the moving object is screen wrapped, reset it to a new
random location
function moveCloud(e:Event) {
random y
location
}
cloud.x = cloud.x - 10; //moves left
if(cloud.x < 0)
{
cloud.y = Math.random() * 400;
cloud.x = 560; //re-set stage right
}
Add Your Event Listeners
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
addEventListener(Event.ENTER_FRAME, moveRocks);
Remove Your Event Listeners!
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
if score == 20)
{
removeEventListener(Event.ENTER_FRAME, moveStuff);
gotoAndStop(3); //win page
}