moving objects. objectives you will know … the stage default dimensions are 550 x 400 px the...

16
Moving Objects

Upload: arline-ramsey

Post on 21-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Moving Objects

Page 2: 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

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

Page 3: 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

Flash Coordinate System

.x is used to move objects from side to side

.y is used to move objects up or down

Page 4: 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

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

Page 5: 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

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

Page 6: 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

Flash Coordinate System

Stage is 5550 px wide and 400 px high (default)

An object moving down has an increasing y coordinate

y-axis g

oes fro

m 0

to

400

Page 7: 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

Flash Coordinate System

Stage is 5550 px wide and 400 px high (default)

An object moving up has an decreasing y coordinate

y-axis g

oes fro

m 0

to

400

Page 8: 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

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)

Page 9: 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

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

Page 10: 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

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 iscloud.x = cloud.x + 10;

Combination Operators+=−=

Page 11: 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

Updating Any Movie Clip Property Continuously (Examples)

Rotate Clockwisehand.rotation += 10;

Rotate Counter-Clockwisewheel.rotation -= 10;

Increase in Sizeshape.width +=10;shape.height += 10;

Move to the Rightcar.x += 5;

Move to the Leftcar.x -= 5;

Decrease in Sizestar.width -= 10;star.height -= 10;

Move Upbullet.y -= 7;

Move Downbullet.y += 7;

Increase Proportionatelyshape.scaleX += .05;shape.scaleY += .05;

Fade Outghost.alpha -= .05;

Fade Inghost.alpha += .05;

Decrease Proportionatelystar.scaleX -= .05;star.scaleY -= .05;

Page 12: 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

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 leftif(cloud.x < 0) {

cloud.x = 560; //re-set stage right

}}

Page 13: 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

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

Page 14: 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

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 leftif(cloud.x < 0) {

cloud.y = Math.random() * 400;

cloud.x = 560; //re-set stage right

}}

random ylocation

Page 15: 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

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);

Page 16: 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

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}