找不到类型或者不是编译时常量?

时间:2013-12-24 20:04:45

标签: actionscript-3 flash

我不明白我的代码有什么问题。当我调试它告诉我:1046:未找到类型或不是编译时常量:LocationChangeEvent。这是代码:

// imports
import flash.events.Event;
import flash.events.LocationChangeEvent;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.MouseEvent;

// setup variables
var _stageWebView:StageWebView;
var myAdvertURL:String = "http://terrypaton.com/ads/exampleAdvert.html";
//
function createAd(event:MouseEvent):void {
    // check that _stageWebView doersn't exist
    if (! _stageWebView) {
        _stageWebView = new StageWebView () ;
        // set the size of the html 'window'
        _stageWebView.viewPort = new Rectangle(0,0,480,80);
        // add a listener for when the content of the StageWebView changes
        _stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE,onLocationChange);
        // start loading the URL;
        _stageWebView.loadURL(myAdvertURL);
    }
    // show the ad by setting it's stage property;
    _stageWebView.stage = stage;
}
function toggleAd(event:MouseEvent):void {
    trace("toggling advert",_stageWebView);
    // check that StageWebView instance exists 
    if (_stageWebView) {
        trace("_stageWebView.stage:"+_stageWebView.stage);
        if (_stageWebView.stage == null) {
            //show the ad by setting the stage parameter
            _stageWebView.stage = stage;
        } else {
            // hide the ad by nulling the stage parameter
            _stageWebView.stage = null;
        }
    } else {
        // ad StageWebView doesn't exist - show create it
        createAd(null);
    }
}

function destroyAd(event:MouseEvent):void {
    // check that the instace of StageWebView exists
    if (_stageWebView) {
        trace("removing advert");
        // destroys the ad
        _stageWebView.stage = null;
        _stageWebView = null;
    }
}

function onLocationChange(event:LocationChangeEvent):void {
    // check that it's not our ad URL loading
    if (_stageWebView.location != myAdvertURL) {
        // destroy the ad as the user has kindly clicked on my ad
        destroyAd(null);
        // Launch a normal browser window with the captured  URL;
        navigateToURL( new URLRequest( event.location ) );
    }
}
// setup button listeners
createAdBtn.addEventListener(MouseEvent.CLICK,createAd);
toggleAdBtn.addEventListener(MouseEvent.CLICK,toggleAd);
destroyAdBtn.addEventListener(MouseEvent.CLICK,destroyAd);

有没有办法更改此代码以使其正常工作,或者此代码不能再使用了?

1 个答案:

答案 0 :(得分:0)

根据引用,如果在AS3中编译,则LocationChangeEvent确实应该在flash.events中。我不确定为什么没找到它。

尝试编译一个更简单的例子:

import flash.events.LocationChangeEvent;
var ev:LocationChangeEvent;