A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: getters and setters not working

  1. #1
    Member
    Join Date
    Apr 2008
    Posts
    30

    getters and setters not working

    Hi, I can't get some getters and setters to work between these two classes so I can't communicate between them. the first class contains the variables for a spaceship to move and the second class is a scrolling class that uses the variables with parallax scrolling for the background. Here is part of the first class here:

    public function update():void
    {
    if (_gameModel.gameMode != "play") return;
    temporaryX = _xPos;
    temporaryY = _yPos;

    vx += acceleration_X;
    vy += acceleration_Y;


    _xPos += vx + gravity_Vx;
    _yPos += vy + gravity_Vy;

    previousX = temporaryX;
    previousY = temporaryY;

    _scroll.update();


    _collision.playerVsGround(this, foreground1);

    }
    public function render():void
    {
    //Draw the object

    //Position object
    _player.x = _xPos;
    _player.y = _yPos;


    }



    public function get vx():Number
    {
    return _xPos - previousX;
    }
    public function set vx(value:Number):void
    {
    previousX = _xPos - value;
    }

    //vy
    public function get vy():Number
    {
    return _yPos - previousY;
    }
    public function set vy(value:Number):void
    {
    previousY = _yPos - value;
    }

    //xPos
    public function get xPos():Number
    {
    return _xPos;
    }
    public function set xPos(value:Number):void
    {
    _xPos = value;

    }

    //yPos
    public function get yPos():Number
    {
    return _yPos;
    }
    public function set yPos(value:Number):void
    {
    _yPos = value;

    }

    //setX
    public function set setX(value:Number):void
    {
    previousX = value - vx;
    _xPos = value;
    }

    //setY
    public function set setY(value:Number):void
    {
    previousY = value - vy;
    _yPos = value;
    }






    ...and the second class....

    public function initializeScroll():void
    {
    //Initialize the scrolling variables
    _rightInnerBoundary
    = (_stage.stageWidth * 0.5) + (_stage.stageWidth * 0.25);
    _leftInnerBoundary
    = (_stage.stageWidth * 0.5) - (_stage.stageWidth * 0.25);
    _topInnerBoundary
    = (_stage.stageHeight * 0.5) - (_stage.stageHeight * 0.25);
    _bottomInnerBoundary
    = (_stage.stageHeight * 0.5) - (_stage.stageHeight * 0.25);

    }
    public function update():void
    {
    var lunarMain:LanderMain = new LanderMain();
    var background3:Bitmap = _gameModel.bg2;
    var background2:Bitmap = _gameModel.bg1;
    var background:Bitmap = _gameModel.foreground1;

    //Capture the current background x and y positions before
    //they're changed by scrolling
    var temporaryX:Number = background.x;
    var temporaryY:Number = background.y;

    var playerWidth:int = 63;
    var playerHeight:int = 53;




    var pHW:Number = playerWidth*.5;
    var pHH:Number = playerHeight*.5;

    //Stop player at inner boundary edges
    if (lunarMain.xPos <= _leftInnerBoundary)
    {
    lunarMain.setX = _leftInnerBoundary;
    _rightInnerBoundary
    = (_stage.stageWidth * 0.5)
    + (_stage.stageWidth * 0.25);
    background.x -= lunarMain.vx;
    background2.x -= lunarMain.vx *.4;
    background3.x -= (lunarMain.vx *.4)*.4;
    }
    else if (lunarMain.xPos >= _rightInnerBoundary)
    {
    lunarMain.setX = _rightInnerBoundary;
    _leftInnerBoundary
    = (_stage.stageWidth * 0.5)
    - (_stage.stageWidth * 0.25);
    background.x -= lunarMain.vx;
    background2.x -= lunarMain.vx *.4;
    background3.x -= (lunarMain.vx *.4)*.4;
    }
    if (lunarMain.yPos <= _topInnerBoundary)
    {
    lunarMain.setY = _topInnerBoundary;
    _bottomInnerBoundary
    = (_stage.stageHeight * 0.5)
    - (_stage.stageHeight * 0.25);
    background.y -= lunarMain.vy;
    background2.y -= lunarMain.vy *.4;
    background3.y -= (lunarMain.vy *.4)*.4;
    }

    else if (lunarMain.yPos >= _bottomInnerBoundary)
    {
    lunarMain.setY = _bottomInnerBoundary;
    _topInnerBoundary
    = (_stage.stageHeight * 0.5)
    - (_stage.stageHeight * 0.25);
    background.y -= lunarMain.vy;
    background2.y -= lunarMain.vy *.4;
    background3.y -= (lunarMain.vy *.4)*.4;
    }
    trace(lunarMain, background.x, background.y, lunarMain.vx, lunarMain.vy, lunarMain.xPos, lunarMain.yPos);

    //Stop background at _stage edges
    if (background.x + background.width < _stage.stageWidth)
    {
    background.x = _stage.stageWidth - background.width;
    _rightInnerBoundary = _stage.stageWidth;
    if(lunarMain.xPos >= _rightInnerBoundary){
    lunarMain.vx = 0;
    }
    }
    else if (background.x > 0)
    {
    background.x = 0;
    _leftInnerBoundary = 0;
    if(lunarMain.x <= _leftInnerBoundary){
    lunarMain.vx = 0;
    }


    }
    if (background.y > 216)
    {
    background.y = 216;
    _topInnerBoundary = 0;
    if(lunarMain.yPos <= _topInnerBoundary){
    lunarMain.vy = 0;
    }

    }
    else if (background.y + background.height < _stage.stageHeight)
    {
    background.y = _stage.stageHeight - background.height;
    _bottomInnerBoundary = _stage.stageHeight;
    if(lunarMain.yPos >= _bottomInnerBoundary){
    lunarMain.vy = 0;
    }
    }
    if (lunarMain.xPos-pHW <= 0)
    {
    lunarMain.xPos = pHW;
    }
    if (lunarMain.xPos+pHW >= _stage.stageWidth)
    {
    lunarMain.xPos = _stage.stageWidth - pHW;
    }

    if (lunarMain.yPos+pHH >= _stage.stageHeight)
    {
    lunarMain.yPos = _stage.stageHeight - pHH;
    }





    scroll_Vx = background.x - temporaryX;
    scroll_Vy = background.y - temporaryY;

    _gameModel.scroll_Vx = scroll_Vx;
    _gameModel.scroll_Vy = scroll_Vy;




    }


    if anyone has a clue why I can't communicate between the classes let me know. I know that is the problem because I traced the second class with this trace statement...trace(lunarMain, background.x, background.y, lunarMain.vx, lunarMain.vy, lunarMain.xPos, lunarMain.yPos); and none of the values are changing, but they change in the LunarMain class. If someone can see or knows the problem let me know.

    Many Thanks,

  2. #2
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Is your update method called every frame ?

    'Cause in the second class you have,
    Code:
    var lunarMain:LanderMain = new LanderMain();
    In your update(), which would create a new instance every frame rather than referencing your actual instance.

    Squize.

  3. #3
    Member
    Join Date
    Apr 2008
    Posts
    30
    thanks but I figured out how to make it work by loading the first class as an argument in update, "update(this)", so now it works, although I don't know why it doesn't work the other way but it doesn't matter, thanks anyway

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center