QuickTip: Scrolling Only in SWF, not on Page.

This entry is part of a series, Quick Tips»

This Quick Tip will show you how to stop simultaneous scrolling on a page and in SWF.

This is just a little test SWF, showing how a SWF would normally act when using the scroll wheel, go ahead and try it out:

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

(I just made a MouseEvent.MOUSE_WHEEL evenlistener on the stage here, then changing the text fields scrollV property)

I know of two methods to solve this problem, I will introduce both of them and show how they are working, then I will tell a little about the difference and lastly show that actually only one of them works! (Thanks to the wonderful FGL forum)

 

Method one:

You need to download the source here, then put it in your library. When you need to use it, import:

import ru.etcs.ui.MouseWheel;

Then in the constructor for your document class call:

MouseWheel.capture ( ) ;

If you want to, you can also release it again, by calling:

MouseWheel.release ( ) ;

and it has a property showing if the mouseWheel is locked or not:

MouseWheel.captured

Here is a test of it (try scrolling now, it still works, but the page won’t scroll anymore):

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

 

Method two:

You need to download the source here, then put it in your library. When you need to use it, import:

import com.spikything.utils.MouseWheelTrap

Then in the constructor for your document class call:

MouseWheelTrap.setup ( stage ) ;

Here is a test of it (it works just as good as the other one it seems).

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

 

So what is the difference?

The big difference is how they do it:

The first one uses the byteArray and localConnection functions in AS3, to create an AS2 SWF that will prevent most browsers from getting the scroll event. (All credits goes to Steven from Yama Games for explaining this.)

The second one is using JavaScript to force the browser to keep the scroll location the same place when someone uses the scroll wheel over the SWF. This is just embedded in the class, not in the page as usually seen. (Again Steven has a expert comment saying it might not work on Facebook – as I still don’t have an account, I can’t go test it out myself).

Bonus info:
When compiling the memory difference is only 708 bytes, so no big difference there (Method 2 is smaller than Method 1).

 

Only one of them works!

So I just tested them all out on Kongregate, and it shows that only method 1 works!
So I will just stick with method one!

You can test it out yourselves here:
Without any script.
With method one.
With method two.

 

Happy coding :)

Posted on January 18th, 2010
Filed under ActionScript 3, Flash, Tutorial |

12 Responses to “QuickTip: Scrolling Only in SWF, not on Page.”

  1. MichaelJW
    January 19th, 2010 at 04:21

    Excellent! Really great to have these in the same place, and nicely explained :)

  2. Mushyrulez
    January 20th, 2010 at 04:33

    Yeah… your links don’t work, they all point to 1-3, you’ll have to edit them to point to 2-3 and 3-3…

    Great tutorial, it’s amazing how many games don’t have this function D:

  3. Rasmus Wriedt Larsen
    January 20th, 2010 at 10:41

    Thanks for the hint Mushy.. I must have forgotten to change the numners (I was in a hurry when I added that part).

  4. MouseWheel - Flashforum
    April 21st, 2010 at 15:04

    […] auch von browser zu browser unterschiedlich sein das da z.b. ist ok: QuickTip: Scrolling Only in SWF, not on Page. | Rasmus Wriedt Larsen – Flash Game Development __________________ die ultimative antwort auf alle programmierfragen: der debugger! – vor eine […]

  5. CS5 (studenten version) kein Scrollen mehr beim browser - Flashforum
    May 25th, 2010 at 11:04

    […] da nach, und probiere die logig umzustellen. also via javascript das browser-scrollen anschalten. QuickTip: Scrolling Only in SWF, not on Page. | Rasmus Wriedt Larsen – Flash Game Development __________________ die ultimative antwort auf alle programmierfragen: der debugger! – vor eine […]

  6. AdoboGames
    June 19th, 2010 at 00:33

    Man, thanks very much for the article. It helped me a lot.

  7. pjbaron
    July 31st, 2010 at 08:24

    Great stuff, thanks for collecting this information and publishing it somewhere handy!
    It’s going to be a life-saver for my Aztec game…
    Any idea why it won’t work on Mac/Safari btw? I’ve been googling but don’t see any mention of problems using AS2 on those machines so I would expect solution #1 to work.

  8. Rasmus Wriedt Larsen
    July 31st, 2010 at 14:09

    Thanks, glad you liked it!

    I don’t know excatly why it won’t work on a Mac, but after checking the site (translated link), I see that the author says it doesn’t work on any browsers on the Mac. But if you can implement some JS on the site where the file is hosted, you should check out this post: http://blog.pixelbreaker.com/flash/swfmacmousewheel.

  9. Tony
    August 13th, 2010 at 22:09

    I wasn’t looking for this – but I am so happy I stumbled upon it. Thanks!

  10. etc
    May 19th, 2011 at 14:54

    It seems that MouseWheel works on Safari/Mac with Flash Player 10.1+.

  11. Rasmus Wriedt Larsen
    May 20th, 2011 at 17:11

    Sounds great!

  12. Zoomen mit dem Scrollrad - Problem - Flashforum
    April 19th, 2013 at 21:32

    […] zum swf. da mag es unterschiede geben. empfehlenswert ist zu wechseln, wie z.b. hier vorgestellt. QuickTip: Scrolling Only in SWF, not on Page. | Rasmus Wriedt Larsen – Flash Game Development sonst bewegt sich etwas im browser und im swf. es sollte aber so sein, dass je nach mausposition […]

Leave a Reply