QuickTip: Scrolling Only in SWF, not on Page.
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:
(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):
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).
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 :)
- QuickTip: Execute Code After Animaiton
- QuickTip: Scrolling Only in SWF, not on Page.
- QuickTip: Managing Saved Data (SharedObject)
- QuickTip: Understanding Right Click Menus (ContextMenu)
- QuickTip: Stopping an Event
- QuickTip: Avoid Firefox popup-blocker When Using navigateToUrl
- QuickTip: Inline If-Then-Else Statement
- QucikTip: Trace an Array With Correct Formatting
Posted on January 18th, 2010 Filed under ActionScript 3, Flash, Tutorial |
12 Responses to “QuickTip: Scrolling Only in SWF, not on Page.”
-
January 19th, 2010 at 04:21 -
January 20th, 2010 at 04:33Yeah… 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:
-
January 20th, 2010 at 10:41Thanks for the hint Mushy.. I must have forgotten to change the numners (I was in a hurry when I added that part).
-
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 […]
-
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 […]
-
June 19th, 2010 at 00:33Man, thanks very much for the article. It helped me a lot.
-
July 31st, 2010 at 08:24Great 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. -
July 31st, 2010 at 14:09Thanks, 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.
-
August 13th, 2010 at 22:09I wasn’t looking for this – but I am so happy I stumbled upon it. Thanks!
-
May 19th, 2011 at 14:54It seems that MouseWheel works on Safari/Mac with Flash Player 10.1+.
-
May 20th, 2011 at 17:11Sounds great!
-
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 […]
Excellent! Really great to have these in the same place, and nicely explained :)