Flex4 Error 1120 when accessing method
I've been banging my head against the wall trying to figure out where I'm going wrong with this, but having no luck.
I'm getting error 1102: 1120: Access of undefined property g. in the following file:
<?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" t开发者_如何学Goitle="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>
My player class:
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;
}
}
}
Any help with why I'm getting this error would be much appreciated.
Apart from setter issue, your code placed directly in Script block. This is the place for class declarations, not the sequential code like in frames. You should put this into function wired to some event - for instance, initialize
event of the component:
<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>
g.name = "name";
a setter function should not be called like a function.
精彩评论