views:

363

answers:

1

I have two problems.

The first problem is with the following functions; when I call the function in (enterFrame), it doesn't work:

onClipEvent (load) {

 function failwhale(levelNum) {
  _root.gotoAndStop("fail");
  failFrom = levelNum;
 }

 function guardSightCollision(guardName, guardSightName) {
  if (_root.guardName.guardSightName.hitTest(_x, _y+radius, true)) {
   failwhale(1);
  }
  if (_root.guardName.guardSightName.hitTest(_x, _y-radius, true)) {
   failwhale(1);
  }
  if (_root.guardName.guardSightName.hitTest(_x-radius, _y, true)) {
   failwhale(1);
  }
  if (_root.guardName.guardSightName.hitTest(_x+radius, _y, true)) {
   failwhale(1);
  }
 }
}
onClipEvent (enterFrame) {
 guardSightCollision(guard1, guard1Sight);
}

Why doesn't it work?...

The second problem lies in the failFrom variable:

function failwhale(levelNum) {
    _root.gotoAndStop("fail");
    failFrom = levelNum;
}

How do I make failFrom a "global" variable in that it can be accessed anywhere (from actionscript in frames and even movieclips)...Right now, when I try to trace failFrom in a different frame, it is "undefined".

A: 

The reason failFrom will be "undefined" from anywhere else you trace it is because you're only declaring it locally in all the functions you've added in your answer. If you wanted to access it like a global variable, you have to assign it to an object that can be accessed anywhere. Like _root.

function failwhale(levelNum) 
{
    _root.failFrom = levelNum;
}

function anotherFunc()
{
    trace( "failed on: " + _root.failFrom );
}

If anotherFunc is called from anywhere else in your script, it should correctly print out failFrom if it has been assigned anything before anotherFunc gets called.

As for your function issue. It looks like you're trying to define functions inside of an event handler. It looks, by your use of onClipEvent, that you're adding this in the timeline. If that's the case, your load event is defining local functions that are no longer available for the on enter frame event. It should just be necessary to have your helper functions always defined:

function failwhale(levelNum) 
{
    //...
}

function guardSightCollision(guardName, guardSightName) 
{
    //...
}

onClipEvent (enterFrame) 
{
    guardSightCollision(guard1, guard1Sight);
}

Since the functions you've added are all in the same scope as the clip event, they should be accessible from enterFrame.

Chuck
Thanks for your suggestions on the _root variable! But, as for the functions, your suggestion gives me a compile error. When I first created these functions, I made them out of any onClipEvent handlers. However, the compiler gave me an error and said I have to have those functions inside an onClipEvent Handler. I've tried putting it in (event) and (load) neither worked.
Joshua