Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Scrolling Background Flash Game Code

Explorer ,
Mar 02, 2017 Mar 02, 2017

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.

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 02, 2017 Mar 02, 2017

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.

Translate
LEGEND ,
Mar 02, 2017 Mar 02, 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='?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 02, 2017 Mar 02, 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?)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2017 Mar 02, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 02, 2017 Mar 02, 2017

If the images are in the Library, which they are, how can I address them in the code, by instance name?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2017 Mar 02, 2017

If they are to be movieclips you could drag the image onto the stage and use Modify/Convert to Symbol to make it be a movieclip. Then in Properties you can give it an instance name.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 02, 2017 Mar 02, 2017

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2017 Mar 02, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 02, 2017 Mar 02, 2017

Removing these lines made the code work! This and some other edits I made after inspecting it over and over here is the final code: [ActionScript 3] WORKING scroller demo - Pastebin.com  and a demo game: Scroller demo 1 - mars714's Sta.sh its laggy but I think if i modify it to be a class again that might fix it. other problems exist, like the character changing direction, but I think thats with the characters timeline....

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2017 Mar 02, 2017

The lagginess won't be any different just by making it be a class file.

You have three problems in your code:

You're only checking the background position if the character has gone too far right. You should put that checking code outside of the check for whether the character has reached the edges.

When going right you test for it being at the edge by adding the width, but the character is animating, so its width is changing. You may have noticed that scrolling is smoother going left. I would just hard code in a value instead of using the character's width.

In a couple of places you are going to labels "ILDEL" and "ILDER". Those are typos, the labels are really IDLEL and IDLER.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 02, 2017 Mar 02, 2017

I fixed the last two, but the first one I am unsure of how to fix, I'm going to read more in my book, I took a break from reading forward and just went over the existing code when I failed to get it working. The next chapter is about object collision, but they might expand on existing code. Even if not it will help me understand more of this particular game code. Thanks for helping me adapt it. If you have any ideas for changing it to check the edges, that be awesome too.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2017 Mar 02, 2017
LATEST

In the version you posted, [ActionScript 3] WORKING scroller demo - Pastebin.com

moving the close brace from line 95 to line 76 would do what I'm suggesting. That way the foreground position is checked if the character is moving left, not just if they are moving right.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines