Copy link to clipboard
Copied
So i've finished learning how to scroll and the book has challenged me to try parallax scrolling and to figure it out on my own. So i've tried to apply my previous scrolling knowledge (as little as it may be) at gave it an honest attempt. The problem is when I test it the player (named after my cat Vivace!) the player doesn't move. I'm also getting the following warnings.
"warning:1090: Migration issue: The onKeyDown event handler is not triggered automatically..."
and
"warning:1090: Migration issue: The onKeyDown event handler is not triggered automatically..."
The code is as follows:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Main extends MovieClip
{
var vx;
var hvx;
var rightInnerBoundary:uint;
var leftInnerBoundary:uint;
var housePage:HousePage;
public function Main();
{
init();
}
function init():void
{
//initilize Variables
vx = 0;
hvx = 0;
rightInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4);
leftInnerBoundary = (stage.stageWidth / 2) - (stage.stageWidth / 4);
addChild(housePage);
//add Event Listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
hvx = -2.5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
hvx = 2.5;
}
}
function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
{
vx = 0;
hvx = 0;
}
}
function onEnterFrame(event:Event):void
{
//initialize local variables
var vivaceHalfWidth:uint = vivace.width / 2;
var backgroundHalfWidth:uint = background.width / 2;
//scroll player
vivace.x = vx;
//stop player at inner boundaries
if (vivace.x - vivaceHalfWidth < leftInnerBoundary)
{
vivace.x = vivaceHalfWidth + leftInnerBoundary;
rightInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4);
background.x -= vx;
front.x -= vx;
clouds.x -= hvx;
}
else if (vivace.x + vivaceHalfWidth > rightInnerBoundary)
{
vivace.x = vivaceHalfWidth - rightInnerBoundary;
leftInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4);
background.x -= vx;
front.x -= vx;
clouds.x -= hvx;
}
//stop background at stage edges
if (background.x + backgroundHalfWidth < stage.stageWidth)
{
background.x = stage.stageWidth - backgroundHalfWidth;
front.x = stage.stageWidth - backgroundHalfWidth;
clouds.x = stage.stageWidth - backgroundHalfWidth;
rightInnerBoundary = stage.stageWidth
}
else if (background.x - backgroundHalfWidth > 0)
{
background.x = 0 + backgroundHalfWidth;
front.x = 0 + backgroundHalfWidth;
clouds.x = 0 + backgroundHalfWidth
leftInnerBoundary = 0;
}
}
}
}
Copy link to clipboard
Copied
onKeyDown is a reserved term in as2. Thus the compiler seems irritated (Migration issue).
rename your Handlers into something more as3-like (keyDownHandler...)
Find more inspiration, events, and resources on the new Adobe Community
Explore Now