Skip to main content
Manic-Mars
Known Participant
March 2, 2017
Answered

Scrolling Background Flash Game Code

  • March 2, 2017
  • 1 reply
  • 1493 views

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:

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.display.DisplayObject;
  4.     import flash.events.KeyboardEvent;
  5.     import flash.ui.Keyboard;
  6.     import flash.events.Event;
  7.     import flash.display.Stage;
  8.     import flash.display.StageAspectRatio;
  9.     import flash.display.MovieClip;
  10.     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.

  1.     class BetterScrolling extends MovieClip {
  2.         [Embed(sorce="Macintosh HD/Users/maya stringer/Documents/Art/Task 1/Animation/games/messing with commands/scroll test",symbol="back")]
  3.         public var BackgroundImage:Class;
  4.         public var backgroundImage:MovieClip = new BackgroundImage();
  5.         public var background:MovieClip = new MovieClip();
  6.        
  7.         [Embed(sorce="Macintosh HD/Users/maya stringer/Documents/Art/Task 1/Animation/games/messing with commands/scroll test.fla",symbol="scroll1_gif")]
  8.         public var ForegroundImage:Class;
  9.         public var foregroundImage:MovieClip = new ForegroundImage();
  10.         public var foreground:MovieClip = new MovieClip();
  11.        
  12.         [Embed(sorce="Macintosh HD/Users/maya stringer/Documents/Art/Task 1/Animation/games/messing with commands/charcterEPORT.fla",symbol="CharacterImage")]
  13.         public var CharacterImage:Class;
  14.         public var characterImage:MovieClip = new CharacterImage();
  15.         public var character:MovieClip = new MovieClip();
  16.        
  17.         public var vx:int = 0;
  18.         public var rightInnerBoundary:uint;
  19.         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...

  1. {
  2.             function BetterScrolling() {
  3.                
  4.              background.addChild(BackgroundImage);
  5.              stage.addChild(background);
  6.              background.x = -(backgroundImage.width - stage.stageWidth) /2;
  7.              background.y = 0;
  8.                
  9.              foreground.addChild(ForegroundImage);
  10.              stage.addChild(foreground);
  11.              foreground.x = -(foregroundImage.width - stage.stageWidth) /2;
  12.              foreground.y = 0;
  13.              character.addChild(CharacterImage);
  14.              stage.addChild(character);
  15.              character.x = 200;
  16.              character.y = 472;
  17.                
  18.              rightInnerBoundary
  19.                 = (stage.stageWidth /2) + (stage.stageWidth /4);
  20.              leftInnerBoundary
  21.                 = (stage.stageWidth /2) - (stage.stageWidth /4);       
  22.             stage.addEventListener
  23.                (KeyboardEvent.KEY_DOWN, keyDownHandler);
  24.             stage.addEventListener
  25.                  (KeyboardEvent.KEY_UP, keyUpHandler);
  26.             stage.addEventListener
  27.                  (Event.ENTER_FRAME, enterFrameHandler);
  28.                
  29.             }

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.

  1.                 function keyDownHandler(event: KeyboardEvent): void
  2.                     {
  3.                
  4.                     if (event.keyCode == Keyboard.LEFT)
  5.                         {
  6.                         vx = -5;
  7.                         character.gotoAndPlay("WALKL");
  8.                     } else if (event.keyCode == Keyboard.RIGHT)
  9.                     {
  10.                         vx = 5;
  11.                         character.gotoAndPlay("WALKR");
  12.                     }
  13.                 }
  14.                 function keyUpHandler(event: KeyboardEvent): void
  15.                 {
  16.                      if (event.keyCode == Keyboard.LEFT)
  17.                          {
  18.                         vx = 0;
  19.                         character.gotoAndPlay("ILDEL");
  20.                         
  21.                     } if (event.keyCode == Keyboard.RIGHT)
  22.                     {
  23.                         vx = 0;
  24.                         character.gotoAndPlay("ILDER");
  25.                        }
  26.                
  27.                     }

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

  1.                         function enterFrameHandler(event: Event): void {
  2.                             {
  3.                                 character.x += vx
  4.                                 if (character.x < leftInnerBoundary)
  5.                                     {
  6.                                     character.x = leftInnerBoundary;
  7.                                     rightInnerBoundary = (stage.stageWidth /2) + (stage.stageWidth /4)
  8.                                     background.x -= vx /2;
  9.                                     foreground.x -= vx;
  10.                                    
  11.                                    
  12.                                 } if (character.x + character.width > rightInnerBoundary)
  13.                                 {
  14.                                     character.x = rightInnerBoundary - character.width
  15.                                     leftInnerBoundary = (stage.stageWidth /2) + (stage.stageWidth /4)
  16.                                     background.x -= vx /2;
  17.                                     foreground.x -= vx;
  18.                                     if (foreground.x > 0)
  19.                                     {
  20.                                         foreground.x = 0;
  21.                                         background.x
  22.                                          = -(background.width - stage.stageWidth) /4;
  23.                                         leftInnerBoundary = 0;
  24.                                     }
  25.                                     if (foreground.x < stage.stageWidth - foreground.width)
  26.                                     {
  27.                                         foreground.x = stage.stageWidth - foreground.width;
  28.                                         background.x
  29.                                          = ((background.width - stage.stageWidth) /4) * -3;
  30.                                         rightInnerBoundary = stage.stageWidth;
  31.                                     }
  32.                                 }
  33.                             }
  34.                         }
  35.                     }
  36.                 }
  37.             }

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.

This topic has been closed for replies.
Correct answer Colin Holgate

Thanks, my current code is here: [ActionScript 3] current scroller code - Pastebin.com  It yields no errors but still does not work. I have no idea why still. All mentions of the imports or embed work have been removed, as that can be done on the stage. I also added a code to attempt to focus the event listeners, but they still do not run... hum.... does it mater that I have objects on multiple layers or that the layers are locked when I export them?


I think you're adapting a Class file to work in the timeline, which is ok, but if you are your code has extra lines. This part

{

  function BetterScrolling() {

look like the beginning of a BetterScrolling class. Those lines wouldn't be needed in the timeline.

1 reply

Colin Holgate
Inspiring
March 2, 2017

It can be tricky adapting Flash Builder code for Flash Pro or Adobe Animate.

You have several lines where it says Embed(sorce="some path, etc. Should those be 'source=' and not 'sorce='?

Manic-Mars
Known Participant
March 2, 2017

Thank you that helped me with the embed code, but now its giving me this error, "Source path yada yada, does not have a recognized extension and a mimeType was not provided." I have no idea what a mimeType is or what has to do with this, do you know where can I look that up? (or even better just know what it is?)

Colin Holgate
Inspiring
March 2, 2017

Look at the example code you were given to make sure the embed path lines are correct.

Or, manually import the images you need into the Library. Flash Builder works that way because it doesn't have a Library, but Animate does. If you can get the images into the Library you can either place them yourself or use code to place them. Either way it's easier that using embed code.