Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Re: Trying to make a simple card matching game (memory)  (Read 12114 times)
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« on: June 01, 2009, 06:27:53 »

I can't redesign your game for you but I can tell you to step back and consider the original premise of MVC.

What does your domain model consist of?

An array of card face values that represent a shuffled deck,
A score,
A list of high scores?
A count of matching attempts? (Not sure what the premise of the game is)

What does your view consist of?

A background
A deck
Some number of displayed cards
A scoreboard
Some buttons?

So the job of PureMVC is to help you get the data from the Model into the View and update the Model based on user interaction at the View.

The Card class you are torn about is probably a view component that displays certain data from the Model - specifically the face value of the card. So it must be informed of the data it is to present. A mediator would do this. When you click on the card, (perhaps to turn it over), the Mediator would listen for this event and in turn either update the Model or perhaps send a notification heard by another mediator or a command that needs to take some action, such as seeing if you clicked on the right card.

-=Cliff>
Logged
Karl Macklin


Email
« Reply #1 on: June 03, 2009, 06:51:42 »

Thanks for the advice so far!

It was a few days ago, and things have gone very far now. The game is close to completion, and will hopefully end up on Kongregate.

As my first attempt at PureMVC, I'm not sure I've understood the benefit of things completely yet.
It has sure been going a LOT better with structuring things compared to my usual way of doing it. Then again, I'm still learning about how events work in flash. I might post the source to my game in the end for those who want to learn more.

In any case.. I bring a question in this post:

Would passing the stage to the constructor of a proxy be considered breaking too far from the norm of decoupling logic and presentation? The reason for me doing this in the game is that I have a GameStateProxy that needs to run a function on each ENTER_FRAME event, and in turn ask a TimeKeeper class to check the getTimer() and whatnot...

Would this be considered a decent approach, or should it be solved another way?
Perhaps just put the ENTER_FRAME event dispatching in the ApplicationFacade itself and use a command to update the Proxy on each frame? What do you think?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #2 on: June 04, 2009, 01:52:58 »

No, you wouldn't want a Proxy knowing about the stage. That is definitely breaking the basic rules of MVC. Instead, consider a Mediator, which can consult the Proxy as need be.

-=Cliff>
Logged
Karl Macklin


Email
« Reply #3 on: June 05, 2009, 10:09:13 »

Gotcha!

My stagemediator now listens for ENTER_FRAME events and calls a gameStateProxy.update() at each.

A DECOUPLER IS YOU!
Logged
Jason MacDonald
Sr. Member
****
Posts: 243


View Profile Email
« Reply #4 on: June 05, 2009, 01:23:39 »

Is it really neseccary to run the update on every enter_frame event? Assuming you are using 30 FPS for your app, that's 30 times a second you would be running gameStateProxy.update(). I don't know what that function does but it can quickly hurt performance doing so. You might want to use a timer if you only need to check once a second or longer.

People really shouldn't ever use enter_frame events unless absolutely neseccary. :)
« Last Edit: June 05, 2009, 01:26:23 by Jason MacDonald » Logged
Karl Macklin


Email
« Reply #5 on: June 05, 2009, 03:07:03 »

Yes I agree with you!

Let me explain:

The update method of my gameStateProxy simply calls timeKeeper.update();

TimeKeeper is a class I made that looks like this:

:
public class TimeKeeper extends EventDispatcher
{
public static const TIME_TICK:String = "timeTick";
public static const POWER_TICK:String = "powerTick";

private const INTERVAL_MS:int = 150;
private var timeStamp:int;
private var powerTimeStamp:int;
private var _gameTimer:Boolean;
private var _powerTimer:Boolean;

public function TimeKeeper()
{
_gameTimer = false;
_powerTimer = false;
}

public function update():void
{
if (_gameTimer)
{
if (getTimer() > (timeStamp + INTERVAL_MS))
{
timeStamp = getTimer();
dispatchEvent(new Event(TimeKeeper.TIME_TICK, true));
}
}

if (_powerTimer)
{
if (getTimer() > (timeStamp + INTERVAL_MS/10))
{
powerTimeStamp = getTimer();
dispatchEvent(new Event(TimeKeeper.POWER_TICK, true));
}
}
}

public function set gameTimer(status:Boolean):void
{
_gameTimer = status;
}

public function get gameTimer():Boolean
{
return _gameTimer;
}

public function set powerTimer(status:Boolean):void
{
_powerTimer = status;
}

public function get powerTimer():Boolean
{
return _powerTimer;
}

public function reset():void
{
timeStamp = getTimer();
powerTimeStamp = getTimer();
}

}

Surely there's probably tons of places where I'm breaking conventions, but what happens is that this function is repeatedly called, and every 15 ms a powerbar is updated (reduced) and the rest of the game runs on the other time that is the whole INTERVAL_MS; 150 ms.

The reason for this is that I need to use getTimer to have a fairly accurate system of measuring time, as this will be a competetive stressful game that deals with time and I want it to be as fair as possible.

This is all based on what I've gathered so far, so there's probably some other better way to do it, but this works surprisingly well.

With my game idle (still running ENTER_FRAME events), the game draws between 0 and 2 % of processing power on my old Core2Duo (E4200, 1.8 ghz).

So yes, I totally agree and I have spent some hours dealing with this issue but I've found this to work pretty well!
Logged
Karl Macklin


Email
« Reply #6 on: June 14, 2009, 01:22:28 »

Game is finished!

http://www.kongregate.com/games/TackleMcClean/memory-mayhem?referrer=TackleMcClean&sfa=permalink

Check it out, and tell me what you think! :)
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #7 on: June 15, 2009, 05:30:40 »

Hey, alright! The old 'concentration' style match-em-up can still be fun. I like the artwork on the cards, especially the wierd googly-eye-stalked alien critter :)

-=Cliff>
Logged
Pages: [1]
Print