SlideServe
Menu
Préférences
© SlideServe · v2026

Interactivity Coding

arty avatar
21 diapositives · 395 Views · Jul 13, 2014

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

Interactivity Coding - slide 1
01 / 21Interactivity Coding
1 / 17

Télécharger la présentation

Interactivity Coding
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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; }