Copy link to clipboard
Copied
Hello once again,
I technically know how to make this work right, but I'm trying to figure out what I want to do won't work right.
I have a Child ("treeToChopDetect") appear when a certain other child ("sceneArray[2]") appears on stage. However, I don't want "treeToChopDetect" to be clickable until another child "guy_Bridge"/ "sceneGuyArray[2]" (the character) reaches frame "guy_AtBridgeNormal" on his own timeline.
However, "treeToChopDetect" seems to be clickable even though the "manager" function is suppose to be constantly telling the "detectArray" that mouseEnabled = false until "sceneGuyArray[2].currentLabel == "guy_AtBridgeNormal"". I keep getting a trace as output if I click on "treeToChopDetect".
I can keep it from being clickable if I set the condition to include "canClickBoo == false" (which it is until the guy reaches the right frame), but I shouldn't have to do that.
For all my other arrays I've been able to use my "manager" function to run through them (via "for" loops) and turn mouseEnabled on and off depending on if the guy is on the right frame, but something isn't working correct with this array, for some reason.
Sorry if that's confusing. I can try to clarify or add more information if necessary. (I have 600+ lines of code so of course I tried to chop out whatever I could.)
The var and array:
var treeToChopDetect:TreeToChopDetect = new TreeToChopDetect;
treeToChopDetect.x = 355;
treeToChopDetect.y = 75;
detectArray = [treeToChopDetect];
The function to detect: (if I add in canClickBoo == true to the stipulation, it runs correctly; however I don't know why I have to do this)
function detectFunction(event:MouseEvent) {
if (treeToChopDetect.hitTestPoint(mouseX, mouseY, true)) {
trace("treeToChopDetect");
}
}
These two functions are run when called in other functions, and they turn on and off the ability to click.
function canClick() {
canClickBoo = true;
canFunBoo = true;
}
function cantClick() {
canClickBoo = false;
canFunBoo = false;
}
main function that adds/removes "outside" children and defines what canClickBoo or canFunBoo means
function manager(event:Event) {
//listen to see what should appear
if (stage.contains(sceneArray[2])) {
addChild(detectArray[0]);
addChild(detectArray[1]);
} else if (!stage.contains(sceneArray[2]) && stage.contains(detectArray[0]) && stage.contains(detectArray[1])) {
removeChild(detectArray[0]);
removeChild(detectArray[1]);
}
//IF canClickBoo is true, able the mouse/turn on buttonMode
if (canClickBoo == true) {
... (various other arrays get turned on)
for (var im:uint=0; im<detectArray.length; im++) {
detectArray[im].mouseEnabled = true;
detectArray[im].buttonMode = true;
}
//ELSE IF canClickBoo is false, disable the mouse/turn off buttonMode
} else if (canClickBoo == false) {
for (var iy:uint=0; iy<detectArray.length; iy++) {
detectArray[iy].mouseEnabled = false;
detectArray[iy].buttonMode = false;
}
}
//if the scene and frame are correct, clicks can be heard
for (var jk:uint = 0; jk<sceneArray.length; jk++) {
if (stage.contains(sceneArray[jk])
&& sceneGuyArray[jk].currentLabel == "guy_At"+wordArray[jk]+"Normal") {
//trace("canClick");
canClick();
// else clicks can't be heard
} else if (stage.contains(sceneArray[jk])
&& sceneGuyArray[jk].currentLabel != "guy_At"+wordArray[jk]+"Normal") {
trace("cantClick");
cantClick();
}
}
}
}
}
}
Copy link to clipboard
Copied
manager() looks like it should be called after canClick() and cantClick() assign their booleans.
Copy link to clipboard
Copied
Technically I have manager listening constantly, along with several other functions:
addEventListener(Event.ENTER_FRAME, sceneAdd);
addEventListener(MouseEvent.CLICK, funFunction);
addEventListener(MouseEvent.CLICK, detectFunction);
addEventListener(Event.ENTER_FRAME, manager);
addEventListener(Event.ENTER_FRAME, itemManager);
I didn't mention that above, sorry. I assume having it called after the assignment of the booleans wouldn't change anything.
If this is too confusing I can just do what worked for me earlier.
EDIT: I think I also achieved what I wanted by only having the MouseEvent listeners listen if canFunBoo = true (I set them inside the "manager" function).
Copy link to clipboard
Copied
Hi, tytbone;
I'm not going to sort through your logic, because, frankly, it's way too complicated. Complicated logic is hard to debug, and the way you've done it is not likely to perform well.
Try something like this:
//Not sure what class you've actually applied to the parent,
//so this name is made up
//Not applying a Class? Maybe you should start.
public class ParentClass extends MovieClip {
//Assuming you've got these on stage an you're going to let Flash populate them
//mainly because logic to create, add, and position things is tedious and I refuse to write it
public var treeToChopDetect:MovieClip;
public var guy_Bridge:MovieClip;
public function ParentClass() {
super();
//added to stage doesn't bubble, use capture
addEventListener(Event.ADDED_TO_STAGE, initNewChild, true);
}
protected function initNewChild(e:Event):void {
var newChild:MovieClip = e.target as MovieClip;
if (newChild) {
switch(newChild.name) {
case 'treeToChopDetect':
mouseChildren = false;
mouseEnabled = false;
break;
case 'guy_Bridge':
//find relevant frame
var frameNum:int = -1;
for each (var frameLabel:FrameLabel in newChild.currentFrameLabels) {
if (frameLabel.name=='guy_AtBridgeNormal') {
frameNum = frameLabel.frame -1;
break;
}
}
if (frameNum > -1) {
//could do this on the timeline of the child mc or with
//a base class...this is just for illustration purposes
//tell guy at bridge to dispatch this event when he gets to the frame
newChild.addFrameScript(frameNum, function(){dispatchEvent(new Event('guy_AtBridgeNormal'))});
//listen for that event so you can unlock the tree
newChild.addEventListener('guy_AtBridgeNormal', unlockTree);
}
}//end case
}//end if the child was actually a movie clip
}//end init
protected function unlockTree(e:Event):void {
guy_Bridge.removeEventListener('guy_AtBridgeNormal', unlockTree);
treeToChopDetect.mouseEnabled = true;
treeToChopDetect.addEventListener(MouseEvent.CLICK, onTreeChop);}
protected function onTreeChop(e:Event):void {
//do stuff
}}
}
Copy link to clipboard
Copied
Amy Blankenship wrote:
...
Hi Amy,
thanks for the response. I'm a bit of a noob at this, so I I'm not familiar with a few things like "addFrameScript", "switch", and "protecting" a function, but I'll continue to read over what you wrote. (I do have a public class applied, though.)
Copy link to clipboard
Copied
AddFrameScript is an undocumented function that adds a frame script. As I said in the inline comment, you could just go into the guy_Bridge clip and physically add it or you could apply a base Class to guy_bridge that handles dispatching the event at the appropriate time. I just used addFrameScript to keep all the code in one place so you could see it.
Protected means that the parent Class can see the property or method, and if you extend the Class the subclasses can see it as well, as opposed to private, which can only be seen by the base Class, and public, which can be seen by anybody (inside or outside the Class). Most people use private for Class members, but I worked in Flex before I worked much in pure Flash, and the Flex team made such bad decisions in the design that trying to find a place where you could alter things when extending a Class was a nightmare. So I make things protected unless I have a really good reason to do otherwise.
Switch is something you should understand if you think that you want to do this stuff for a living.
To summarize what my solution does differently than yours is, rather than continually monitoring to see what frame guy_Bridge is on, we listen for an event that is dispatched at the correct frame and then enable the mouse on the tree (only once). At that point, we quit listening for that event anymore, since we've accomplished what we needed to.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now