Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help how do I do this? Coding aspect to it. ActionScript 3 - Exercise 1 The foll

ID: 3530527 • Letter: H

Question

Help how do I do this? Coding aspect to it.


ActionScript 3 - Exercise 1

The following is the code I have so far, I need to add the following:

- Keyboard Control

- Screen Wrapping

---------------------------------------

----------------------------------------

package{

import flash.display.*;

import flash.events.MouseEvent;


public class Main extends MovieClip {

public function Main(){

stage.addEventListener(MouseEvent.CLICK, clickHandler);

}

private function clickHandler(event:MouseEvent):void {

stage.addChild(CartoonAnimation);

animate.x = 400;

animate.y =400;

animate.gotoAndPlay(1);

}

}

}


Explanation / Answer

package{

import flash.display.*;

import flash.events.*;


public class Main extends MovieClip {

public function Main(){

stage.addEventListener(MouseEvent.CLICK, clickHandler);

// the below code for keyboard event handling

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

//ScreenWrapping(CartoonAnimation);

}

private function clickHandler(event:MouseEvent):void {

stage.addChild(CartoonAnimation);

animate.x = 400;

animate.y =400;

animate.gotoAndPlay(1);

}

private function keyDownHandler(event:KeyboardEvent):void {

trace("keyDownHandler: " + event.keyCode);

}


private function keyUpHandler(event:KeyboardEvent):void {

trace("keyUpHandler: " + event.keyCode);

}

// call this below function when you want to screen wrap an animation movieclip .

// for example,

//ScreenWrapping(CartoonAnimation);

private function ScreenWrapping(object:MovieClip) {

//1.

var objectHalfWidth:uint=object.width/2;

var objectHalfHeight:uint=object.height/2;

//2.

if (object.x - objectHalfWidth>stage.stageWidth) {

object.x=0-objectHalfWidth;

}

else if (object.x + objectHalfWidth <0) {

object.x=stage.stageWidth+objectHalfWidth;

}

//3.

if (object.y + objectHalfHeight<0) {

object.y=stage.stageHeight+objectHalfHeight;

}

else if (object.y - objectHalfHeight > stage.stageHeight) {

object.y=0-objectHalfHeight;

}

}

}

}