|
-
AS on timeline works - not in Classes...
I've got a project working with all the AS in one keyframe on the timeline. Lovely. Then I thought this should probably be in packages and classes. That's where it went wrong.
I managed to fix a number of errors, but got really really ...REALLY stuck when it came to adding event listeners and using arrays.
Basically, the project involves a number of movieclips (rooms) being moved onto the stage with left/right arrows, with each movieclip containing other movieclips that you can interact with (mouse over to display text, etc).
If anyone could take a minute to let me know what I'm doing wrong I would be very grateful. I've been working in AS for a while, but this is my first major venture in AS3 and using Classes. Sorry about the length of this...
Here's the code that worked, on the timeline, all in one piece -
Code:
// Flash - import Tween class
import fl.transitions.Tween; //added to as
import fl.transitions.TweenEvent;//added to as
import fl.transitions.easing.*;//added to as
var leftGutter:Number = -2880;
var rightGutter:Number = 2880;
var room:Array = new Array();
room[1] = livingroom_mc;
room[2] = mediaroom_mc;
room[3] = gallery_mc;
room[4] = musicroom_mc;
var lastRoom:Number = 4;/* put this in external file all by itself. Stops going over edge of world*/
var oldRoom:Number = new Number ();
var newRoom:Number = new Number ();
var currentRoom:Number = 1; /* between 1 and lastRoom */
darkenRooms();
startListening(); /* event listeners for room nav */
function nextRoom(event:MouseEvent):void
{
if (currentRoom < lastRoom )
{
oldRoom = currentRoom;
currentRoom ++;
stopListening();
room[currentRoom].alpha = 1; /* lights on in current room */
room[currentRoom].y = 0;
var getNextRoom:Tween = new Tween(room[currentRoom],"x",Strong.easeOut,rightGutter,0,2,true);
var moveOldRoom:Tween = new Tween(room[oldRoom],"x",Strong.easeOut,0,leftGutter,2,true);
getNextRoom.addEventListener(TweenEvent.MOTION_FINISH, turnOffOldRoom); /*turn off lights only when New Room is getted */
function turnOffOldRoom(event:TweenEvent):void
{
var darkenOldRoom:Tween = new Tween(room[oldRoom],"alpha",Strong.easeOut,1, 0, 1,true);
darkenOldRoom.addEventListener(TweenEvent.MOTION_FINISH, ears);
}
}
}
function prevRoom(event:MouseEvent):void
{
if (currentRoom >1)
{
oldRoom = currentRoom;
currentRoom --;
stopListening();
room[currentRoom].alpha = 1; /* lights on in current room */
room[currentRoom].y = 0;
var getPreviousRoom:Tween = new Tween(room[currentRoom],"x",Strong.easeOut,leftGutter,0,2,true);
var moveThisRoom:Tween = new Tween(room[oldRoom],"x",Strong.easeOut,0,rightGutter,2,true);
moveThisRoom.addEventListener(TweenEvent.MOTION_FINISH, turnOffPreviousRoom);/*turn off lights only when next room is finished */
function turnOffPreviousRoom(event:TweenEvent):void
{
var fadeThisRoom:Tween = new Tween(room[oldRoom],"alpha",Strong.easeOut,1, 0, 1,true);
fadeThisRoom.addEventListener(TweenEvent.MOTION_FINISH, ears);
}
}
}
function stopListening():void
{
trace('Cant hear you ');
/* turn off room nav buttons */
nextRoom_btn.removeEventListener(MouseEvent.CLICK, nextRoom);
prevRoom_btn.removeEventListener(MouseEvent.CLICK, prevRoom);
}
function ears(event:TweenEvent):void
{
startListening();
}
function startListening():void
{
trace('Im Listening ');
/*Get the previous/next buttons */
if (currentRoom != lastRoom)
{
nextRoom_btn.addEventListener(MouseEvent.CLICK, nextRoom);
}
else
{
removeChild(nextRoom_btn);
addChild(prevRoom_btn);
}
if (currentRoom >1)
{
prevRoom_btn.addEventListener(MouseEvent.CLICK, prevRoom);
}
else
{
removeChild(prevRoom_btn);
addChild(nextRoom_btn);
}
if (currentRoom > 1 && currentRoom < lastRoom)
{
addChild(nextRoom_btn);
addChild(prevRoom_btn);
}
}
function darkenRooms():void
{
for (var i:uint = 2;i<=lastRoom; i++)
{
room[i].alpha = 0; /* all other rooms are dark */
}
}
/* Navigation */
menuDetect_btn.addEventListener(MouseEvent.MOUSE_OVER, MenuUp);
var menuOff:Number = 2100;
var menuOn:Number = 2008;
function MenuUp(event:MouseEvent):void
{
var showMenu:Tween = new Tween(menu_mc,"y",Strong.easeOut,menuOff,menuOn,1,true);
menuDetect_btn.removeEventListener(MouseEvent.MOUSE_OVER, MenuUp);
menu_mc.menuClose_btn.addEventListener(MouseEvent.MOUSE_OVER, MenuDown);
menu_mc.home_btn.addEventListener(MouseEvent.CLICK, goHome);
menu_mc.about_btn.addEventListener(MouseEvent.CLICK, goAbout);
menu_mc.portfolio_btn.addEventListener(MouseEvent.CLICK, goRoom);
}
function MenuDown(event:MouseEvent):void
{
var showMenu:Tween = new Tween(menu_mc,"y",Strong.easeOut,menuOn,menuOff,1,true);
menu_mc.menuClose_btn.removeEventListener(MouseEvent.MOUSE_OUT, MenuDown);
menuDetect_btn.addEventListener(MouseEvent.MOUSE_OVER, MenuUp);
}
function goHome(event:MouseEvent):void
{
if (currentRoom >1)
{
oldRoom = currentRoom;
currentRoom = 1;
stopListening();
room[currentRoom].alpha = 1; /* lights on in current room */
room[currentRoom].y = 0;
var getHome:Tween = new Tween(room[1],"x",Strong.easeOut,leftGutter,0,2,true);
var moveThisRoom:Tween = new Tween(room[oldRoom],"x",Strong.easeOut,0,rightGutter,2,true);
moveThisRoom.addEventListener(TweenEvent.MOTION_FINISH, turnOffPreviousRoom);/*turn off lights only when next room is finished */
function turnOffPreviousRoom(event:TweenEvent):void
{
var fadeThisRoom:Tween = new Tween(room[oldRoom],"alpha",Strong.easeOut,1, 0, 1,true);
fadeThisRoom.addEventListener(TweenEvent.MOTION_FINISH, ears);
}
}
}
function goRoom(event:MouseEvent):void
{
/*fade out old room, fade in new one */
}
function goAbout(event:MouseEvent):void
{
if (currentRoom >1)
{
oldRoom = currentRoom;
currentRoom = 1;
stopListening();
room[currentRoom].alpha = 1; /* lights on in current room */
room[currentRoom].y = 0;
var getHome:Tween = new Tween(room[1],"x",Strong.easeOut,leftGutter,0,2,true);
var moveThisRoom:Tween = new Tween(room[oldRoom],"x",Strong.easeOut,0,rightGutter,2,true);
moveThisRoom.addEventListener(TweenEvent.MOTION_FINISH, turnOffPreviousRoom);/*turn off lights only when next room is finished */
function turnOffPreviousRoom(event:TweenEvent):void
{
var fadeThisRoom:Tween = new Tween(room[oldRoom],"alpha",Strong.easeOut,1, 0, 1,true);
fadeThisRoom.addEventListener(TweenEvent.MOTION_FINISH, ears);
}
}
var f:int = (5);
livingroom_mc.sign_mc.gotoAndStop(5);
}
// NICK MOVIE CLIP INTERACTION //
var playing:Boolean = false;
livingroom_mc.nick_mc.stop();
livingroom_mc.nvideo_btn.addEventListener(MouseEvent.CLICK, PlayNick);
function StopNick(event:MouseEvent):void
{
livingroom_mc.nick_mc.stop();
livingroom_mc.nvideo_btn.removeEventListener(MouseEvent.CLICK, StopNick);
livingroom_mc.nvideo_btn.addEventListener(MouseEvent.CLICK, PlayNick);
playing = false;
trace(playing);
}
function PlayNick(event:MouseEvent):void
{
livingroom_mc.nick_mc.play();
livingroom_mc.nvideo_btn.removeEventListener(MouseEvent.CLICK, PlayNick);
livingroom_mc.nvideo_btn.addEventListener(MouseEvent.CLICK, StopNick);
playing = true;
trace(playing);
}
// SIGN INTERACTION //
livingroom_mc.sign_mc.buttonMode = true;
livingroom_mc.sign_mc.addEventListener(MouseEvent.MOUSE_OVER, signOn);
livingroom_mc.sign_mc.addEventListener(MouseEvent.MOUSE_OUT, signOff);
livingroom_mc.sign_mc.addEventListener(MouseEvent.MOUSE_DOWN, readSign);
function signOn(event:MouseEvent):void
{
livingroom_mc.sign_mc.gotoAndStop(2);
}
function signOff(event:MouseEvent):void
{
livingroom_mc.sign_mc.gotoAndStop(1);
}
function readSign(event:MouseEvent):void
{
trace('hey im reading');
livingroom_mc.sign_mc.removeEventListener(MouseEvent.MOUSE_OVER, signOn);
livingroom_mc.sign_mc.removeEventListener(MouseEvent.MOUSE_OUT, signOff);
livingroom_mc.sign_mc.removeEventListener(MouseEvent.MOUSE_DOWN, readSign);
livingroom_mc.sign_mc.gotoAndStop(5);
livingroom_mc.sign_mc.nextText_mc.addEventListener(MouseEvent.CLICK, nextText);
livingroom_mc.sign_mc.prevText_mc.addEventListener(MouseEvent.CLICK, prevText);
var f:int = (5);
function nextText(event:MouseEvent):void
{
if (f < 10)
{
f = f+5;
}
else
{
f = 10;
}
livingroom_mc.sign_mc.gotoAndStop(f);
trace(f);
}
function prevText(event:MouseEvent):void
{
if (f > 5)
{
f = f-5;
}
else
{
f = 1;
livingroom_mc.sign_mc.addEventListener(MouseEvent.MOUSE_OVER, signOn);
livingroom_mc.sign_mc.addEventListener(MouseEvent.MOUSE_OUT, signOff);
livingroom_mc.sign_mc.addEventListener(MouseEvent.MOUSE_DOWN, readSign);
}
livingroom_mc.sign_mc.gotoAndStop(f);
trace(f);
}
}
I set the document class to Rooms. This is Rooms.as:
Code:
package com
{
import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.text.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.display.MovieClip;
import fl.transitions.TweenEvent;
import flash.display.SimpleButton;
/* import as files from folder
import scripts.secondary_file;
*/
public class Rooms extends MovieClip
{
public var leftGutter;
public var rightGutter;
public var room:Array;
public var lastRoom:Number;
public var oldRoom:Number;
public var newRoom:Number;
public var currentRoom:Number; /* between 1 and lastRoom */
public var getNextRoom:Tween;
public var moveOldRoom:Tween;
public var darkenOldRoom:Tween;
public var getPreviousRoom:Tween;
public var moveThisRoom:Tween;
public var fadeThisRoom:Tween;
public var i:int;
public var livingroom_mc:MovieClip;
public var mediaroom_mc:MovieClip;
public var gallery_mc:MovieClip;
public var musicroom_mc:MovieClip;
public var nextRoom_mc:MovieClip;
public var prevRoom_mc:MovieClip;
public var n:MovieClip;
public var p:MovieClip;
public function Rooms()
{
lastRoom = 4;
currentRoom = 1;
darkenRooms();
leftGutter = -2880;
rightGutter = 2880;
startListening(); /* event listeners for room nav */
room[1] = livingroom_mc;
room[2] = mediaroom_mc;
room[3] = gallery_mc;
room[4] = musicroom_mc;
}
public function nextRoom(event:MouseEvent):void
{
if (currentRoom < lastRoom )
{
oldRoom = currentRoom;
currentRoom ++;
stopListening();
room[currentRoom].alpha = 1; /* lights on in current room */
room[currentRoom].y = 0;
getNextRoom = room[currentRoom],"x",Strong.easeOut,rightGutter,0,2,true;
moveOldRoom = room[oldRoom],"x",Strong.easeOut,0,leftGutter,2,true;
getNextRoom.addEventListener(TweenEvent.MOTION_FINISH, turnOffOldRoom); /*turn off lights only when New Room is getted */
function turnOffOldRoom(event:TweenEvent):void
{
darkenOldRoom = room[oldRoom],"alpha",Strong.easeOut,1, 0, 1,true;
darkenOldRoom.addEventListener(TweenEvent.MOTION_FINISH, ears);
}
}
}
public function prevRoom(event:MouseEvent):void
{
if (currentRoom >1)
{
oldRoom = currentRoom;
currentRoom --;
stopListening();
room[currentRoom].alpha = 1; /* lights on in current room */
room[currentRoom].y = 0;
getPreviousRoom = room[currentRoom],"x",Strong.easeOut,leftGutter,0,2,true;
moveThisRoom = room[oldRoom],"x",Strong.easeOut,0,rightGutter,2,true;
moveThisRoom.addEventListener(TweenEvent.MOTION_FINISH, turnOffPreviousRoom);/*turn off lights only when next room is finished */
function turnOffPreviousRoom(event:TweenEvent):void
{
fadeThisRoom = room[oldRoom],"alpha",Strong.easeOut,1, 0, 1,true;
fadeThisRoom.addEventListener(TweenEvent.MOTION_FINISH, ears);
}
}
}
public function stopListening():void
{
trace('Cant hear you ');
/* turn off room nav buttons */
nextRoom_mc.removeEventListener(MouseEvent.CLICK, nextRoom);
prevRoom_mc.removeEventListener(MouseEvent.CLICK, prevRoom);
}
public function ears(event:TweenEvent):void
{
startListening();
}
public function startListening():void
{
trace('Im Listening ');
/*Get the previous/next buttons */
if (currentRoom != lastRoom)
{
nextRoom_mc.addEventListener(MouseEvent.CLICK, nextRoom);
nextRoom_mc.buttonMode = true;
}
else
{
nextRoom_mc.visible = false;
prevRoom_mc.visible = true;
}
if (currentRoom >1)
{
prevRoom_mc.addEventListener(MouseEvent.CLICK, prevRoom);
prevRoom_mc.buttonMode = true;
}
else
{
removeChild(prevRoom_mc);
addChild(nextRoom_mc);
}
if (currentRoom > 1 && currentRoom < lastRoom)
{
addChild(nextRoom_mc);
addChild(prevRoom_mc);
}
}
private function darkenRooms():void
{
for (i = 2;i <= lastRoom; i++)
{
room[i].alpha = 0; /* all other rooms are dark */
}
}
}
}
As you may be able to tell, the arrays dont' work....but they are an important part, as I need at time to be able to tell where visitors are.
Other functions from the original got sorted into livingRoom.as and menu.as,for the bottom navigation. I guess I could start with Room.as and go from there.
I would be very grateful to anyone who can help point me in the right direction. I feel as though this is a structural problem rather than syntax.
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|