Hello, thank you for taking the time to read my question.
This is my first time using Flash to create a website. I am not familiar with Action Script, its functions classes etc., however I am familiar in programming in JAVA.
I will be using ActionScript 3 and Flash CS5. (I have Flash CS4 also if needed)
I am trying to create a website with a scrolling background that has 4 different scenes.
I have attached an image to illustrate my idea.
There will be a user interface, consisting of 4 buttons, which will be on stage at all times.
Each of the button will correspond to a different scene of the background. So if the user clicks on the third button, the background will slide itself until the third scene appears on the stage.
Questions in mind:
How would the coding be like? I am planning to apply it to the button. So the background should shift ___ amount of pixels to the left or right depending on which scene the user is currently in. So if background is currently at position 800,0 (so the user is currently in scene 2) and the user clicks on 4th button. The background should move -1600 so the 4th scene will appear on stage. I am guessing there is 16 different cases I have to address.
Assuming if that works, how would I also move the other objects on the scenes of background? Such as links to email address or my portfolio.
Assuming that your first scene starts at (0, 0) coordinates and you know either how many pixels each screen occupies or where exactly that screen starts (in coordinates) you can simply do something like:
var screenWidth:Number = 800; // Seems like that's the number you want
private function onGreenButtonClick(e:MouseEvent):void
{
this.background.x = - (1 * screenWidth); // 0 for first screen, 1 for second screen,...
}
this.greenButton.addEventListener(MouseEvent.CLICK , this.onGreenButtonClick);
or
var positions:Array = [0, 800, 1600, 2400];
private function onGreenButtonClick(e:MouseEvent):void
{
this.background.x = - positions[1]; // 0 for first screen, 1 for second screen,...
}
this.greenButton.addEventListener(MouseEvent.CLICK , this.onGreenButtonClick);
Of course this will shift instantly and this is very basic way...
If you want animated way, check out Tween class
Also I would need a way to tell which scene the user is currently at in order to correctly determine whether to move left or right and how much. If the user is already in scene 2 and wants to move to scene 4, the background would only need to move -1600. If the user is in scene 1 and wants to move scene 4, the background would need to move 2400.
Would the following be correct? Please correct any typos in the coding. I am a bit rusty in my programming.
Clicking on the green button should take the user to scene 2
The following code is for the green button
var positions:Array = [0, 800, 1600, 2400];
if (this.background.x == positions[0]){ //User is in first scene
private function onGreenButtonClick(e:MouseEvent):void
{
this.background.x = positions[1]; //moves user to second scene from first scene
}
}
if (this.background.x == positions[1]){ //User is in second screen
private function onGreenButtonClick(e:MouseEvent):void
{
this.background.x = positions[0]; //moves user to second scene from second scene which is no movement since they are already in second scene
}
}
Forget about current screen.... you need to go to 4th screen - set the background.x coordinate to appropriate value... in this case this.background.x = -2400; that's it... nothing else... If background is currently at x:0 or x:-1600 it still will have to end up at x:-2400 to show that same 4th screen.
Assuming if that works, how would I also move the other objects on the scenes of background? Such as links to email address or my portfolio.
if you want to move your content along with the background, add your content to the background as children. you can actually have four Sprites to hold each screen separately and with ease of access for you...
Code:
import flash.display.Sprite;
private var background:Sprite = new Sprite(); // Can be movieclip if you really need it
// Scenes:
private var blueScene:Sprite = new Sprite();
private var greenScene:Sprite = new Sprite();
private var orangeScene:Sprite = new Sprite();
private var redScene:Sprite = new Sprite();
// Setup scenes:
this.blueScene.x = 0;
this.greenScene.x = 800;
this.orangeScene.x = 1600;
this.redScene.x = 2400;
this.blueScene.y = 0;
this.greenScene.y = 0;
this.orangeScene.y = 0;
this.redScene.y = 0;
this.blueScene.width = this.greenScene.width = this.orangeScene.width = this.redScene.width = 800;
this.blueScene.height = this.greenScene.height = this.orangeScene.height = this.redScene.height = 600;
this.background.addChild(this.blueScene);
this.background.addChild(this.greenScene);
this.background.addChild(this.orangeScene);
this.background.addChild(this.redScene);
Now you have four different containers for your content. Add your stuff to each scene sprite as if they were exactly 800x600 pages separately...
P.S. That previous scrolling need's not to be updated... :P you still scroll the background itself and everything within will go along for a ride...
I'm working with Flex builder, thus ALL of my code is withing a class... thus i'm always using "this" where applicable and always putting private/protected/public/internal...
Since you're putting your code directly into actions panel in frame, should lose at least private modifiers (that's what the error is about... )
Humm after compiling, I get an output
TypeError: Error #1010: A term is undefined and has no properties.
at portfolio_fla::MainTimeline/frame75()
So I ran the debugger and it stops at the red text above this.home.addEventListener...
I double checked all my buttons to see if they had the right corresponding names and they did. Then I tried to change them to movieclips but that did not work either.
Here is what I did to make my buttons. I imported a psd image and changed that into a button type. I also applied frames in the UP OVER and DOWN to animate the button. (I used this http://www.smartwebby.com/Flash/button_symbols.asp) Would that cause a conflict with the addEventListener(MouseEvent.CLICK, this.onButtonClick ?
Last edited by coldmail590; 05-25-2010 at 03:01 AM.
Let's see... are your buttons on the stage? or are they inside some other clip?
if on stage and have instance names this should work (without "this." before onButtonClick)... If inside some other movieclip, then you need to specify a path to that button.
For the sake of example say that your buttons are within background movieclip.
then you would access those buttons from stage using
Yay no errors or outputs! BUT the buttons do not work
When click on any of them (the first time) it will bring me to scene 3 (I did not notice any tween or easing). When I try to click on another button, nothing happens.
Thank you very much for your test version. I found out that my symbol properties in the library were different than yours. They had a Linkage with Export for ActionScript and Export in frame 1 both checked. After unchecking those, the buttons worked. However the coordinates were off. My background image is positioned like yours on the stage but when I compile it to test, the background positions were off by 800.
But anyways, everything seems to be working fine now! Thank you very much for your help! I will now attempt to add contents to the background and have them also move along with it.
If you add something to background and/or it's children, don't touch that scrolling algorithm. Those items will move along like a pencil put on the paper moves along when you pull paper. Anyway... Glad to be of help... good luck
Hello again! I am back with more questions I am confused with the sprites. Here is the code I am using:
PHP Code:
import flash.display.Sprite;
var bg_mc:Sprite = new Sprite(); // Can be movieclip if you really need it
// Scenes:
var blueScene:Sprite = new Sprite();
var greenScene:Sprite = new Sprite();
var orangeScene:Sprite = new Sprite();
var redScene:Sprite = new Sprite();
//My button to link to flickr.com
var flickr_button:Sprite = new Sprite();
Hi... I'm almost always here to help if only i can..
First about blueScene, redScene object... I suggested those to sort of have pages.. You should have your color background in a blueScene, redScene, etc. object and all the content for that scene as well... background is only for scrolling scenes and aranging them... if you do so, 1) you don't have to wory about scene offset in the background (x:800, x:1600, etc) and 2) it would be quite easy to switch scene places cause you wouldn't have to rewrite all your content coordinates...
if you don't want to put content into those blueScene, redScene, etc. objects, then you should just remove them for cleaner code and no useless objects.
Second: Sprite... Sprite doesn't initially have any content inside, thus it's size is widht:0 and height:0... you might reconsider creating button programmatically using new Sprite(), because then you have to either draw something inside and/or draw something inside... for ease of use, you should probably put a button on stage using tools in Flash... would save a lot of nerves i guess... trust me... i'm dealing with such things all the time, cause i'm not using Flash application... i'm just writing AS3 code and all drawing is done with code in my little project..
Third: that error i think is unrelated to the piece of code you posted... i don't see a single SimpleButton (or it's derivative) instances... all i see are Sprites... so i don't see a way this code could produce this error... try to debug and find out where this error pops up... also there's something in frame 75...