访问方法时Flex4错误1120

时间:2011-03-28 06:34:45

标签: actionscript-3 flex flex4

我一直在撞墙,试图找出我出错的地方,但没有运气。

我收到错误1102: 1120:访问未定义的属性g。在以下文件中:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="Home">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import ca.ss44.pabhi.Player;

            var g:Player = new Player();
            g.name( "name" );

        ]]>
    </fx:Script>
</s:View>

我的玩家类:

package ca.ss44.pabhi
{
    public class Player
    {
        private var _name:String; //Players name
        private var _cards:Array;

        public function Player()
        {
            //Sets up the current player, players be default have no cards.
            _cards = new Array();
        }

        /**
         * Retrn an array of all cards that the play currently holds.
         */
        public function getCards():Array{ return new Array(); }

        //Adds a card to the players
        public function addCard( card:Card ):void{

        }

        //Drop a card from the players hand.
        public function dropCard( card:Card ):void{

        }

        public function set name( value:String ):void{
            _name = value;
        }

        public function get name():String{
            return _name;
        }

    }
}

对于我收到此错误的原因有任何帮助,我们将不胜感激。

2 个答案:

答案 0 :(得分:1)

g.name = "name";

不应像函数一样调用setter函数。

答案 1 :(得分:1)

除了setter问题,您的代码直接放在Script块中。这是类声明的地方,而不是帧中的顺序代码。您应该将此功能连接到某个事件 - 例如,组件的initialize事件:

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="Home"
        initialize="init()">
<fx:Script>
        <![CDATA[
            private function init():void
            {
                import ca.ss44.pabhi.Player;
                var g:Player = new Player();
                g.name = "name";
            }
        ]]>
    </fx:Script>