PDA

View Full Version : Document Class Variables



FlashMapper
January 6th, 2008, 07:37 PM
I have a document class setting a variable called "_captioning", being set to false. I have a base class being added that needs to check and see what this variable is set to when it is added to the stage. I can't get it to read this variable at all.

I've been doing some research and found a post on this site talking about MovieClip(root) for this same problem. I can't get it to work at all. MovieClip(root)._captioning will trace from in the document class but not the base class. If I try even tracing MovieClip(root) all I get is "null". If I trace MovieClip(root) from the document class, I get [object MainEngineDocClass] (MainEngineDocClass is the name of my document class).

This thing is driving me crazy. Can anyone help?

Dazzer
January 6th, 2008, 10:12 PM
don't.

Pass the required variable to the particular class. The whole point of a class is that it is oblivious to its surroundings and is as independent as possible.

Good OOP aside, to solve your problem...

Make sure your variable is declared Public. Or that there is a function that retrieves the function for you. You could also try having a look at static variables, which is pretty much pseudo-global variables.

FlashMapper
January 6th, 2008, 10:26 PM
The other reason I'm trying to get the class to talk to the variable is because by clicking on the button, I want to change the captioning variable from false to true. I thought I'd go basic by just trying to get the class to read the variable first.

Dazzer
January 6th, 2008, 10:40 PM
Well think of it this way

A button is a button. All you need to know if it has been clicked or not. A button should not know what its supposed to do. Simply handle states.

That is why event listeners become useful. A single button can have many different implementation depending on what is handling its events.

So in this case, your button dispatches the MOUSE_UP event, you handle that event, and then in your MovieThingie class, you could have a "toggleCaptions()" method, which you can call. Inside your MovieThingie class you would store this Captions variable, and toggle it using that method.

Now your variables are all properly encapsulated, and requires no external influence to function.



function yourButtonListener(e:MouseEvent):void{
yourMovieThingie.toggleCaptionss();
}




//inside your movieThingie Class
private var _captions:Boolean = false;

function toggleCaptions():void{
this._captions = !this._captions.
}

FlashMapper
January 7th, 2008, 06:28 AM
Thank you. That is useful. I went to bed last night thinking about how I should be rearranging my file around to not need those variables.

The one question I would have then that I don't completely get about OOP is a situation where I'm adding particles to the stage, and they need to check on certain conditions, like gravity or friction or something, variables controlled by the user, to determine how they're supposed to act. Do I have something central like a container class the particles and the buttons can all affect? Classes can talk to each other, right?

kmintmier
January 7th, 2008, 12:30 PM
Yes, classes can definitely communicate with one another -- as long as they can "see" each other. That is, one class can reference the static parts of another class (as long as the other class has been imported) and it can access any instance of the other class as long as it has access to a reference to that instance.

To illustrate the "static" part of that statement, if you have a class called Environment with public static constants called GRAVITY and FRICTION, instances of a Particle class can always reference Environment.GRAVITY and Environment.FRICTION (again, given that Environment has been imported into Particle).

In reference to your "container" question, suppose your Particle instances know how to respond to gravity and friction through a function called update(). Instead of writing the update() function to reference the gravity and friction of your environment, you could work the other direction -- that is, let the environment reference (and pass values to) your particles. Your Environment class could be composed of an array of Particle objects, and you could put an update() function in the Environment class that uses a loop to call "myParticles[particleId].update(_gravity, _friction);", where gravity and friction are private variables in an instance of the Environment class.

So, to recap, your particles could be "agnostic" to the current state of the environment. Whenever a particle needs moved, the necessary parameters could be passed *to* the particle, not pulled *from* the environment. Make sense?

I hope this is useful in some way. I haven't had a much of a chance to contribute lately. :)

FlashMapper
January 7th, 2008, 08:09 PM
I'm having trouble again based of one of Dazzer's posts. I'm having my document class load 2 classes. A captioning class that puts a block on the stage. I also have a button class that puts a button on the stage. The button class has a mouse up event. The captioning class has a function that will make itself invisible. I can't get my button to make that captioning class run its function to make itself invisible.

Am I supposed to have the captioning block listening for the click of that button? There's still something I'm missing or not understanding.

FlashMapper
January 7th, 2008, 11:32 PM
Here's the evolved problem I'm having. Basically, I've got 2 classes. One is dispatching an event and one is supposed to be receiving it.

Here's the button class dispatching the event:

package com.jmspark.mainEngine
{
import flash.display.MovieClip;
import flash.events.*;

public class ButtonSetOptions extends MovieClip
{
public function ButtonSetOptions()
{
addEventListener(MouseEvent.CLICK, turnOn)
}

public function turnOn(event:MouseEvent):void{
gotoAndStop("CLICKED")

dispatchEvent(new Event("ChangeCaptioning"));

removeEventListener(MouseEvent.CLICK, turnOn)
}
}
}

Here's the class for the listener:

package com.jmspark.mainEngine
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
import jmspark.mainEngine.ButtonSetOptions;

public class CaptioningClass extends MovieClip
{

public function CaptioningClass()
{
addEventListener("ChangeCaptioning", toggleCaptioning);
}

public function toggleCaptioning(event:Event):void
{
trace("God Help Me");
}
}
}

I know I'm missing something in the class for the listener. I believe somewhere I need to establish the ButtonSetOption class so that the listener knows what to listen for...right?