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)) {
failwha开发者_JAVA百科le(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".
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.
Instead of putting the value on _root, you can also put it on _global. When you don't specify scope explicitly, it /appears/ that a local value (in function/on object) will override _root, and then _root overrides _global. I see various things use _global instead of _root, but I am unsure as to any real benefit.
If you want your function definitions on an instance try this:
onClipEvent (load)
{
this.failwhale = function (levelNum)
{
_root.gotoAndStop("fail");
failFrom = levelNum;
}
this.guardSightCollision = function(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)
{
this.guardSightCollision(guard1, guard1Sight);
}
Otherwise, you should put them out on the timeline of the parent object (or the root timeline) and reference the object direction like:
myobject.failwhale = function (levelNum)
{
_root.gotoAndStop("fail");
failFrom = levelNum;
}
myobject.guardSightCollision = function(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);
}
}
myobject.onEnterFrame = function()
{
this.guardSightCollision(guard1, guard1Sight);
}
Also, I would recommend strongly typing your variables/arguments for both compile error checking and some speed increase.
精彩评论