Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7521237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:12:09+00:00 2026-05-30T02:12:09+00:00

Background: I recently completed a tic-tac-toe game in AS3, using some simple function-based code

  • 0

Background:
I recently completed a tic-tac-toe game in AS3, using some simple function-based code I had written in C many years ago.
Now I’m on a quest to do it the “right way” using OOP techniques and best practices.
Everything is now divided into neat little packages, it looks pretty, and the last part of my journey is to get all the little buggers to communicate with each other.

My dilemma:
I want to move the code which holds the game state from my main to it’s own class in com.okaygraphics.model.GameState.
The problem is nearly every other package gets and sets these game state properties.
I’m trying to figure out the simplest way to encapsulate this stuff, while still allowing my other classes to access it.

Where I’m at:

package com.okaygraphics.model{

    public class GameState {

        private var _player1State:uint=0x00000000;
        private var _player2State:uint=0x00000000;
        private var _activePlayer:int=0;


        public function get p1GameState():uint {
            return _player1State;
        }
        public function set p1GameState(value:uint):void {
            _player1State = value;
        }

        public function get p2GameState():uint {
            return _player2State;
        }
        public function set p2gameState(value:uint):void {
            _player2State = value;
        }

        public function get activePlayer():int {
            return _activePlayer
        }
        public function set activePlayer(value:int):void {
            _activePlayer = value;
        }
    }
}

Qestions:

1) Do I need a constructor? I mean, my program will never have more than one GameState. If I should call my getters/setters as instance methods, how do I get each other class to reference the SAME instance from their respective packages?

2) Do I even need getters and setters? Perhaps the class could just have 3 public properties? If so, how would I acheive the proper scope with regard to my other classes?

3) Should I assign everything to the class itself using the static keyword? If so, how would I implement and use those static methods?

4) Is this a mistake? Did I totally just program myself into a corner?

This seems like a pretty common task, yet one that I still don’t fully understand. A brief explaination, some links, or the name of the technique I need would be greatly appreciated.

Thanks in advance,
-Max

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:12:11+00:00Added an answer on May 30, 2026 at 2:12 am

    You could approach this several different ways.

    You could make GameState a Singleton. That’ll ensure that any instance of GameState is the same instance. Here’s how to do that:

    private static var _gameState:GameState;
    private static var _allowInstantiation:Boolean;
    
    public function GameState()
    {
        if (!_allowInstantiation)
        {
            throw new Error("GameState is a Singleton. Use GameState.getInstance() instead of new GameState().");
        }
        else
        {
            _allowInstantiation = false;
        }
    }
    
    public static function getInstance():GameState
    {
        if (!_gameState)
        {
            _allowInstantiation = true;
            _gameState = new GameState();
        }
    
        return _gameState;
    }
    

    Then, anytime you want to get GameState, you use this syntax:

    var _gameState:GameState = GameState.getInstance();
    

    By using a Singleton, you can still use the getters and setters you’ve already made, but the Singleton will ensure that every class that accesses GameState will be accessing the same instance.

    Or, if you don’t want to use a Singleton, you could make those private vars public static vars instead:

    public static var player1State:uint=0x00000000;
    public static var player2State:uint=0x00000000;
    public static var activePlayer:int=0;
    

    You’ll no longer need the getters/setters, and you’d set them from other classes like this:

    GameState.player1State = 0xFF0000;
    GameState.player2State = 0xFF0000;
    GameState.activePlayer = 1;
    

    Either way you set up the Model, remember to put thought into how it’ll be communicated with by the Controller and View. Remember that separation of Model and View is one of the main goals of the MVC pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I am writing and using a very simple CGI-based (Perl) content management tool
I recently created a ListView using ListAdapter and applied a static background image behind
Recently, my friend helped me fix a background overlay problem. I had an issue
I recently tried applying a gradient background to a webpage using only CSS3. while
My database background is mainly Oracle, but I've recently been helping with some SQL
Background: I had an issue with my Rails server recently where it would stop
Background: Recently while looking at a structured text editor I noticed they used a
I recently got into Java. I have a background in dynamic languages and I'm
I am from procedure programming background, end up writing TSQL recently with my new
Background: At my company we are developing a bunch applications that are using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.