Scrolling Background Flash Game Code
I am at my wits end! I have tried everything to get this code to work, I bought this book because it had a very good review, and have done everything I can to get the code and content to match whats in the book. I have only made two major modifications and even when I remove them the code fails. I will go over the code and my changes, as well as the few actual reported errors, mostly it just fails to work and doesn't report why...
Opening code:
- package {
- import flash.display.Sprite;
- import flash.display.DisplayObject;
- import flash.events.KeyboardEvent;
- import flash.ui.Keyboard;
- import flash.events.Event;
- import flash.display.Stage;
- import flash.display.StageAspectRatio;
- import flash.display.MovieClip;
- import flash.display.FrameLabel;
Class code: the book is meant for use with photoshop and flash builder, I am using Animate, but at this point I am trying everything to get the code flawless. I have the three embed files checked export for runtime sharing, and the url matches. The difference here is the symbol code is not in my book, as the import is from a direct image, also a big change is I change BetterScrolling extends Sprite, to extends MovieClip. This is because I need the Character image to change position when the player moves. right and left.
- class BetterScrolling extends MovieClip {
- [Embed(sorce="Macintosh HD/Users/maya stringer/Documents/Art/Task 1/Animation/games/messing with commands/scroll test",symbol="back")]
- public var BackgroundImage:Class;
- public var backgroundImage:MovieClip = new BackgroundImage();
- public var background:MovieClip = new MovieClip();
- [Embed(sorce="Macintosh HD/Users/maya stringer/Documents/Art/Task 1/Animation/games/messing with commands/scroll test.fla",symbol="scroll1_gif")]
- public var ForegroundImage:Class;
- public var foregroundImage:MovieClip = new ForegroundImage();
- public var foreground:MovieClip = new MovieClip();
- [Embed(sorce="Macintosh HD/Users/maya stringer/Documents/Art/Task 1/Animation/games/messing with commands/charcterEPORT.fla",symbol="CharacterImage")]
- public var CharacterImage:Class;
- public var characterImage:MovieClip = new CharacterImage();
- public var character:MovieClip = new MovieClip();
- public var vx:int = 0;
- public var rightInnerBoundary:uint;
- public var leftInnerBoundary:uint;
next section: this defines the locations of the objects. IDK if its needed other than the Inner Boundaries and event listeners... I'd love to know how to define the inner boundaries with exact pixels...
- {
- function BetterScrolling() {
- background.addChild(BackgroundImage);
- stage.addChild(background);
- background.x = -(backgroundImage.width - stage.stageWidth) /2;
- background.y = 0;
- foreground.addChild(ForegroundImage);
- stage.addChild(foreground);
- foreground.x = -(foregroundImage.width - stage.stageWidth) /2;
- foreground.y = 0;
- character.addChild(CharacterImage);
- stage.addChild(character);
- character.x = 200;
- character.y = 472;
- rightInnerBoundary
- = (stage.stageWidth /2) + (stage.stageWidth /4);
- leftInnerBoundary
- = (stage.stageWidth /2) - (stage.stageWidth /4);
- stage.addEventListener
- (KeyboardEvent.KEY_DOWN, keyDownHandler);
- stage.addEventListener
- (KeyboardEvent.KEY_UP, keyUpHandler);
- stage.addEventListener
- (Event.ENTER_FRAME, enterFrameHandler);
- }
these are the Key handlers and where a major change comes in: I placed frame labels on the inside of the movie clip Character, contained within are additional movie clips with space to play under each frame label, or a stop(); on top to hold it in place.
- function keyDownHandler(event: KeyboardEvent): void
- {
- if (event.keyCode == Keyboard.LEFT)
- {
- vx = -5;
- character.gotoAndPlay("WALKL");
- } else if (event.keyCode == Keyboard.RIGHT)
- {
- vx = 5;
- character.gotoAndPlay("WALKR");
- }
- }
- function keyUpHandler(event: KeyboardEvent): void
- {
- if (event.keyCode == Keyboard.LEFT)
- {
- vx = 0;
- character.gotoAndPlay("ILDEL");
- } if (event.keyCode == Keyboard.RIGHT)
- {
- vx = 0;
- character.gotoAndPlay("ILDER");
- }
- }
enter frame handler: I have never gotten the VX or VY code to ever work int the past. I've used substitute codes like speed codes to work instead. That it seems that the inner boundaries don't seem to be taking...
- function enterFrameHandler(event: Event): void {
- {
- character.x += vx
- if (character.x < leftInnerBoundary)
- {
- character.x = leftInnerBoundary;
- rightInnerBoundary = (stage.stageWidth /2) + (stage.stageWidth /4)
- background.x -= vx /2;
- foreground.x -= vx;
- } if (character.x + character.width > rightInnerBoundary)
- {
- character.x = rightInnerBoundary - character.width
- leftInnerBoundary = (stage.stageWidth /2) + (stage.stageWidth /4)
- background.x -= vx /2;
- foreground.x -= vx;
- if (foreground.x > 0)
- {
- foreground.x = 0;
- background.x
- = -(background.width - stage.stageWidth) /4;
- leftInnerBoundary = 0;
- }
- if (foreground.x < stage.stageWidth - foreground.width)
- {
- foreground.x = stage.stageWidth - foreground.width;
- background.x
- = ((background.width - stage.stageWidth) /4) * -3;
- rightInnerBoundary = stage.stageWidth;
- }
- }
- }
- }
- }
- }
- }
apologies for this being very long, but I can't even address the errors, since it seems it wont tell me them unless I put the code in the main file rather than a action script class file and try to export. I really hope someone can help, Ive been asking over at stack overflow and they seem to have much more about java, and I just can't find the answers I need there.