Interactivity Coding

21
Interactivity Coding 9 Topic

description

9. Interactivity Coding. Topic. Objectives of this topic:. You can …. write high-level AS code that:. dynamically places the object on screen. move and alters the object on screen. Overview. Introduction Creating a Drag-and-Drop class Detecting Collisions Responding to Collisions - PowerPoint PPT Presentation

Transcript of Interactivity Coding

Page 1: Interactivity Coding

Interactivity Coding

9Topic

Page 2: Interactivity Coding

Objectives of this Objectives of this topic:topic:

write high-level AS code that:

You can …

dynamically places the object on screen

move and alters the object on screen

Page 3: Interactivity Coding

OverviewOverviewIntroductionCreating a Drag-and-Drop classDetecting CollisionsResponding to CollisionsDetecting WinRandomly Placing Objects

Page 4: Interactivity Coding

IntroductionIntroductionUnlike basic scripting,

programming with AS allows the designer to create highly interactive and entertaining applications that can be infinitely modified

Page 5: Interactivity Coding

Creating a Drag-and-Drop Creating a Drag-and-Drop ClassClass Creating drag and drop

functionality in Flash CS3 is quite different than older versions.

Drag-and-drop is simple application that involve dragging any board.

Example of Drag-and-Drop interactivity

Page 6: Interactivity Coding

Display objects have methods call startDrag and stopDrag that make it easy to add this type of interactivity to any application.◦startDrag method is called when the

mouse is pressed down over the board◦stopDrag methods is called when the

mouse is let up over the boardBoth are built-in methods of the

Sprite class

Page 7: Interactivity Coding

board.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);

private function dragMovie(event:MouseEvent):void{

board.startDrag();}

board.addEventListener(MouseEvent.MOUSE_UP, dropMovie);

private function dropMovie(event:MouseEvent):void{

board.stopDrag();}

Page 8: Interactivity Coding

More drag-and-drop in Flash CS3

http://www.flashandmath.com/basic/dragdroptour/dd_tour1.html

Page 9: Interactivity Coding

Detecting CollisionsDetecting CollisionsUsing AS code to detect whether

the object is touching a target object when the objects are dragged into the target area

Create the target object on stage by giving an instance name to this object

Page 10: Interactivity Coding

Define a public variable with the data type asterisk (*), which this variable will accept multiple data types:

Public var _targetPiece:*;

Page 11: Interactivity Coding

Define a function or event handler to detect whether the object is dropping in the target area (two objects are touching)

private function checkTarget(event:MouseEvent):void{

if(event.currentTarget.hitTestObject(event.currentTarget._targetPiece)){<body> }

}

Page 12: Interactivity Coding

hitTestObject() is a method to determines whether an object is touching or hitting another object passed in through the parentheses that contain:◦the object that user have just released

event.currentTarget◦the object related the target object

event.currentTarget._targetPiece

Page 13: Interactivity Coding

Responding to CollisionsResponding to CollisionsDetermine the responds when a

user successfully drops an object in the correct area and handle the occasions when they don’t.

Define a public variables that will hold the original X and Y positions

Page 14: Interactivity Coding

public var _origX:Number;public var _origY:Number;

….

_origX = this.x;_origY = this.y;

Page 15: Interactivity Coding

Assign the target object to user’s object

Otherwise, set the X and Y values of the object back to their original X and Y values if there is not match

Page 16: Interactivity Coding

if(event.currentTarget.hitTestObject(event.currentTarget._targetPiece))

{event.currentTarget.x = event.currentTarget._targetPiece.x;event.currentTarget.y = event.currentTarget._targetPiece.y;

} else {

event.currentTarget.x = event.currentTarget._origXevent.currentTarget.y = event.currentTarget._origY;

}

Page 17: Interactivity Coding

Fix the objects on their target placesevent.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,

checkTarget);

public function disable():void{

this.removeEventListener(MouseEvent.MOUSE_DOWN, dragMovie);

this.removeEventListener(MouseEvent.MOUSE_UP, dropMovie);this.buttonMode = false;

}

Page 18: Interactivity Coding

Detecting a WinDetecting a WinSet up the game to detect a win

when the game successfully completed

Define two variables to hold ◦the total possible matches ◦The number of current matches the

user has made◦Write a conditional statement that

runs if the possible matches are greater or equal to the total matches

Page 19: Interactivity Coding

private var _totalPieces:Number;private var _currentPieces:Number;

…_totalPieces = 6;_currentPieces = 0;

…_currentPieces ++;if(_currentPieces >= _totalPieces){

trace("You Win");}

Page 20: Interactivity Coding

Randomly Placing ObjectRandomly Placing ObjectCreate a function to randomize

the placement of the object on screen

randomPosition(<object>);

Page 21: Interactivity Coding

private function randomPosition(piece:*):void{

piece.x = Math.round(Math.random() * (255 – piece.width));piece.y = Math.round(Math.random() * (400 –

piece.height));piece._origX = piece.x; //back to random positionpiece._origY = piece.y;

}