1 / 21

Interactivity Coding

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

arty
Télécharger la présentation

Interactivity Coding

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 9 Interactivity Coding Topic

  2. 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

  3. Overview • Introduction • Creating a Drag-and-Drop class • Detecting Collisions • Responding to Collisions • Detecting Win • Randomly Placing Objects

  4. Introduction • Unlike basic scripting, programming with AS allows the designer to create highly interactive and entertaining applications that can be infinitely modified

  5. Creating a Drag-and-Drop Class • 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

  6. 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 board • Both are built-in methods of the Sprite class

  7. 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(); }

  8. More drag-and-drop in Flash CS3 http://www.flashandmath.com/basic/dragdroptour/dd_tour1.html

  9. Detecting Collisions • Using 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

  10. Define a public variable with the data type asterisk (*), which this variable will accept multiple data types: Public var _targetPiece:*;

  11. 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> } }

  12. 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

  13. Responding to Collisions • Determine 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

  14. public var _origX:Number; public var _origY:Number; …. _origX = this.x; _origY = this.y;

  15. 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

  16. if(event.currentTarget.hitTestObject(event.currentTarget._targetPiece))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._origX event.currentTarget.y = event.currentTarget._origY; }

  17. Fix the objects on their target places event.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; }

  18. Detecting a Win • Set 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

  19. private var _totalPieces:Number; private var _currentPieces:Number; … _totalPieces = 6; _currentPieces = 0; … _currentPieces ++; if(_currentPieces >= _totalPieces) { trace("You Win"); }

  20. Randomly Placing Object • Create a function to randomize the placement of the object on screen randomPosition(<object>);

  21. 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 position piece._origY = piece.y; }

More Related