actionscript 3.0类型强制错误

时间:2011-11-25 04:25:05

标签: flash actionscript-3 google-maps

我正在尝试通过单击动画片段移动到另一个帧。我已经检查了框架名称等,但每当我尝试单击我的MovieClip时,它总是会出现这样的错误。我的应用程序是将谷歌地图与闪存相结合。因此,当我点击动画片段时,它应该移动到包含谷歌地图的另一个帧。

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@2675cf99 to flash.display.MovieClip.
at startMap/movetoMap()

这是我的代码:

package  {

import flash.display.MovieClip;
import flash.events.MouseEvent;
import com.google.maps.*;
import flash.geom.Point;
import flash.events.Event;
import com.google.maps.overlays.Marker;
import com.google.maps.services.ClientGeocoder;
import com.google.maps.services.GeocodingEvent;

public class startMap extends MovieClip {

    var gMap : Map = new Map();

    public function startMap() {

        this.x = 700;
        this.y = 150;
        this.scaleX = 0.5;
        this.scaleY = 0.5;
        this.addEventListener(MouseEvent.CLICK, movetoMap);
    }

    function movetoMap (e : MouseEvent)
    {
//I think this is where the error took place...
        MovieClip(this.parent).gotoAndStop("mymap"); // move to another frame

                    //the code below is for the next frame
        //trace ("initiating map");
        /*
        gMap.key = "ABQIAAAAkvJLDXCdl31EuFDEitKQ6hTDVs7mYo4hdRoqkWYrrPdtz_Eb9RRJP9mw3bPiboGSX4c0stQsYo4aPQ";
        gMap.sensor = "true";
        gMap.x = 100;
        gMap.y = 50;
        gMap.setSize(new Point(stage.width - 200, stage.height - 100));
        gMap.addEventListener(MapEvent.MAP_READY, prepareMap);
        gMap.addEventListener(MapMouseEvent.CLICK, showPoint);
        stage.addChild (gMap);
        */
    }
    /*
    function prepareMap (e : Event)
    {
        doGeoCode ("Jakarta, Indonesia");
    }

    function showPoint (e : MapMouseEvent)
    {
        var revGeoCode : ClientGeocoder = new ClientGeocoder();
        revGeoCode.addEventListener(GeocodingEvent.GEOCODING_SUCCESS,
                function markPlace (e : GeocodingEvent) {
                    var place : Array = e.response.placemarks;
                    var marker : Marker = new Marker (place[0].point);
                    gMap.addOverlay(marker);
                    gMap.setZoom(8, true);
                    gMap.setCenter (marker.getLatLng());
                    marker.addEventListener(MapMouseEvent.CLICK,
                            function showInfo(e : MapMouseEvent){
                                gMap.openInfoWindow(marker.getLatLng(),
                                    new InfoWindowOptions ({title:"Welcome to", content:place[0].address}));
                            });
                });
        revGeoCode.addEventListener(GeocodingEvent.GEOCODING_FAILURE, 
                function addFailure(e : GeocodingEvent){
                    trace ('fail to geocode');
                });
        revGeoCode.reverseGeocode(e.latLng);
    }

    function doGeoCode (placeName : String)
    {
        var placeInfo : ClientGeocoder = new ClientGeocoder();
        placeInfo.addEventListener(GeocodingEvent.GEOCODING_SUCCESS, 
                function addInfo(e : GeocodingEvent){
                    var place : Array = e.response.placemarks;
                    var mark : Marker = new Marker (place[0].point);
                    gMap.setCenter(mark.getLatLng());
                    gMap.setZoom(8, true);
                    gMap.addOverlay(mark);
                    mark.addEventListener(MapMouseEvent.CLICK,
                            function setPlaceInfo (e : MapMouseEvent){
                                gMap.openInfoWindow(place[0].point,
                                    new InfoWindowOptions ({title:"Welcome to", content:place[0].address}));
                            });
                });
        placeInfo.addEventListener(GeocodingEvent.GEOCODING_FAILURE, 
                function addFailure(e : GeocodingEvent){
                    trace ('fail to geocode');
                });
        placeInfo.geocode(placeName);
    }
    */
}

}

THX之前提供任何帮助......

2 个答案:

答案 0 :(得分:1)

startMap直接附加到舞台上,舞台类不会从MovieClip继承。

您需要将MovieClip(this.parent).gotoAndStop("mymap");更改为gotoAndStop("mymap");,或者如果您要将startMap添加到舞台,而不是使用stage.addChild(startMapObj);添加startMap,则应该this.addChild(startMapObj);

答案 1 :(得分:0)

moveToMap方法的范围是startMap类本身(我猜测它是添加到根阶段对象的MovieClip)。因此this.parent将解析为舞台对象,并且通过尝试将其强制转换为其他类型(MovieClip),您将获得类型强制错误。

mymap帧标签实际上是否在根时间轴上?如果要操纵根时间轴,则必须使用Stage的root属性(并将其强制转换为MovieClip)。所以在你的moveToMap函数中看起来有点像这样:

MovieClip(stage.root).gotoAndStop("mymap");

(请注意,如果stage已添加到显示列表中,您只能访问DisplayObject属性。