Adobe Flash ActionScript 2 if statement _currentframe - if-statement

For a class assignment, I'm creating a game. I want to make it so when you score enough points, you'll proceed to the next frame. However, the code I put int he Actions layer won't work. Whenever I score enough points, it just stays on the same frame. What am I doing wrong?
This is the code I used:
if(score_mc._currentframe == 6){
_root.gotoAndStop("next");
}
Adobe Flash CS6
ActionScript 2.0

Looks like you use score_mc.nextFrame(); or something like that for increment you score. You could add this check you made right after this incrementation. So it will be
score_mc.nextFrame();
if(score_mc._currentframe == 6){
_root.gotoAndStop("next");
}

Related

Cocos2D-X most efficient way to select the appropriate sprite sheet

I have 3 sets of sprite sheets for my first scene.
I'm using XCode to develop for iOS using Cocos2D-X.
The first sprite sheet has an extension -sd,
the second sprite sheet has -hd,
and the third set has 3 sprite sheets (using Multipack in Texture Packer Pro) with the extension -ipadhd.
I followed a tutorial and I found a method that goes like this
CCString* file = (Utils::getArtScaleFactor() > 1) ? CCString::create("randomFile-hd.plist") : CCString::create("randomFile-sd.plist");
//The Utils::getArtScaleFactor function
float Utils::getArtScaleFactor()
{
return artScaleFactor;
}
1- Is there a similar way to choose between 3 files instead of 2?
2- Is this the common method for choosing the appropriate file size-wise?
3- This question is somewhat off the topic I was discussing but I really need an answer to it too: I have an animation and its frames are split into 3 sprite sheets, how do I cache 3 .plist files? And if that's impossible, what are my options?
Hope I provided all the necessary information!
Regards
Sure, but that ternary operator should probably be switched out with another construct (like a switch) for clarity and flexibility.
For example:
// Always init your pointers.
CCString* file = NULL;
switch (Utils::getArtScaleFactor())
{
// Scale factor shouldn't ever be 0, but we should account for it
// so it doesn't get picked up by the default case.
case 0:
case 1:
file = CCString::create("randomFile-sd.plist");
break;
case 2:
file = CCString::create("randomFile-hd.plist");
break;
// Scale factors 3 and above get the largest resource.
default:
file = CCString::create("randomFile-super-hd.plist");
break;
}
Yes, this type of logic is on the wiki, except they use if-else for three resource sizes. I've seen it on the forums and tutorials, too.
According to this thread on the cocos2d-iphone forums, yes:
CCSprite can display CCSpriteFrames with different textures.
So there’s no problem at all even to use animation that mixes frames from different textures (spritesheets) - Stepan Generalov
The implementations posted there are in obj-c but you can convert the cocos2d api calls to cpp easily.

How to colorize selections in ncurses?

I'm working on a Roguelike right now in ncurses and C++. Right now I'm coding the title screen, it looks something like this:
game name
company name
(n)ew game
(q)uit
But I'd really like for the user to be able to use the arrow keys to highlight their selection and maybe reuse this functionality later for the inventory screen. The problem is I can't figure out how to colorize new game and not quit when its selected and vice versa. So far my code is like this:
mvaddstr((height-1)/2, ((width-4)/2)-(newgame_button.length()/2),newgame_button.c_str());
mvaddstr((height+1)/2, ((width-4)/2)-(quit_button.length()/2),quit_button.c_str());
mvaddstr((height-10)/2, ((width-4)/2)-(titlename.length()/2), titlename.c_str());
mvaddstr((height-8)/2, ((width-4)/2)-(companyname.length()/2), companyname.c_str());
Then I have my key handler. I tried to do it like this:
if(ch == KEY_DOWN) {
start_color();
init_pair(1, COLOR_BLUE, COLOR_BLACK);
attron(COLOR_PAIR(1));
attroff(COLOR_PAIR(1));
}
But it doesn't work. I'm pretty new to ncurses so it could be something totally obvious that I am overlooking. Thanks!
The best/easiest way to accomplish simple menu is do redraw title screen every time you change your selection( like press rp_arrow down_arrow, usually ). I don't have much time to refresh my ncurses so here's pseudocode. You associate variable holding selection with text that has to be highlighted. And it goes like that.
#selecion=0;
#while(key_pressed != ENTER)
#print game title
#print company name
#if selection = 0 print highlighted new game
#else print new game without highlight
#if selection = 1 print highlighted quit
#else print quit without highlight
#if uparrow selection++
#if downarrow selection--
I know it's not perfect and you have to work your logic of getting input and drawing fresh screen but that's just a general idea behind simple, highlighted menu.
If you need any help just write here and I'll dig deeper into some of my code/memory for details and tips. Good luck with your game!

C++ parallel loops

I'm making a console game called alien spaceships as a homework. It should look something like this http://img74.imageshack.us/img74/8362/alieninvadersfdcl192720mu1.jpg .
So far so good I ain't allowed to use classes nor objects => only functions and arrays.
I have one while loop that checks the buttons I press on the keyboard and according to the button applies some functions.
The problem comes when I try to shoot a missle because it's done with a "for" loop and when I shoot I can't move. Can someone give me an idea how the model is supposed to look like and how can I make something like this work. I don't think it's needed to post my code, but if you want I'll post it.
I assume that you're not willing to play with multiple threads. It is not mandatory for a simple game like this and would add a bit of complexity.
So generic loop for monothreaded game is:
state new_state = createInitialState();
do
{
input = readInput(); // non blocking !
new_state = modifyState(input, new_state);
updateScreen(new_state);
}
while (!exitCondition(input));
None of these functions should loop for long.
In your case, the missile position should be updated in modifyState taking into account the time since the last modifyState.
I assume you use a matrix to store all the data, and periodically you print the content of the matrix (that's how you create a console game).
So, your code should look something like this:
render()
{
update_position(x,y);
if(missile_fired)
update_missile_position();
}
main()
{
for(;;)
{
read_input(&x,&y);
render();
draw_image();
}
}

Is it bad practice to have nested render loops?

I'm porting a game from Ruby to C++. There is a main render loop that updates and draw the content. Now let's say that during the game, you want to select an item another screen. The way it's done in the original code is to do Item item = getItemFromMenu(); getItemFromMenu is a function that will open the menu and do have its own update/render loop, which mean that during the whole time the player has this other screen open, you are in a nested render loop. I feel like this is a bad method but I'm not sure why. On the other hand it's very handy because I can open the menu with just 1 function call and so the code is localized.
Any idea if this is a bad design or not?
I hesitated to post it on gamedev, but since this is mostly a design issue I posted it here
edit : some pseudo-code to give you an idea:
The usual loop in the main part of the code:
while(open) {
UpdateGame();
DrawGame();
}
now inside UpdateGame() i would do something like:
if(keyPressed == "I") {
Item& item = getItemFromInventory();
}
And getItemFromInventory():
while(true) {
UpdateInventory();
if(item_selected) return item;
DrawInventory();
}
A good way to handle something like this would be to replace the DrawInventory() call with something like InvalidateInventory(), which will mark the current graphical state of the inventory as outdated and request it to be redrawn during the next frame rendering (which'll happen pretty soon after when the main loop gets to DrawGame()).
This way, you can keep running through the main loop, but the only parts of the screen that get looked at for redrawing are the ones that have been invalidated, and during normal gameplay you can invalidate your (2/3)D environment as a normal part of processing, but then inside the inventory you can always mark only inventory assets as needing to be redrawn, which minimises overhead.
The other part of your inner loop, UpdateInventory(), can be a part of UpdateGame() if you use a flag to indicate the current game state, something like:
UpdateGame()
{
switch(gameState)
{
case INVENTORY:
UpdateInventory();
break;
case MAIN:
default:
UpdateMain();
break;
}
}
If you really wanted, you could also apply this to drawing:
DrawGame()
{
switch(gameState)
{
case INVENTORY:
DrawInventory();
break;
case MAIN:
default:
DrawMain();
break;
}
}
But I think drawing should be encapsulated and you should tell it which part of the screen, rather than which separate area of the game, needs to be drawn.
What you've created with your nested render loop is functionally a state machine (as most game render loops tend to be). The problem with the nested loop is that many times you'll want to do the same sorts of things in your nested loop as your outer loop (process input, handle IO, update debug info etc).
I've found that it's better to have one render loop and use a finite state machine (FSM) to represent your actual states. Your states might look like:
Main menu state
Options menu state
Inventory state
World view state
You hook up transitions between states to move between them. The player clicking a button might trigger the transition which could play an animation or otherwise, then move to the new state. With a FSM your loop might look like:
while (!LeaveGame()) {
input = GetInput();
timeInfo = GetTimeInfo();
StateMachine.UpdateCurrentState(input, timeInfo);
StateMachine.Draw();
}
A full FSM can be a bit heavyweight for a small game so you can try a simplified state machine using a stack of game states. Every time the user does an action to transition to a new state you push the state on a stack. Likewise when they leave a state you pop it off. Only the top of the stack typically receives input and the other items on the stack may/may not draw (depending on your preference). This is a common approach and has some upsides and downsides depending on who you talk to.
The simplest option of all is to just throw a switch statement in to pick which render function to use (similar to darvids0n's answer). If you're writing an arcade clone or a small puzzle game that would do just fine.

Button held down by code

I am writing a game and am trying to make a person move with the arrow keys.
I have this code
if (Key_Down(DIK_DOWN))
{movedown(player)}
This works but as I want the player to take four steps every time the key is pressed I created and animation loop. so the player cannot input any more move commands until the animation is over and they have taken four steps I made the code this.
if(player.move == false)
{
if (Key_Down(DIK_DOWN))
{movedown(player)}
}
The problem is that now once a button is pressed the program acts like the button is held down and the player keeps moving until another direction is pressed.
Can anyone explain what the outer loop has done to the code and fix the problem ?
it is programmed in visual c++ 2005 and direct x 9.c
Edit:
If I remove the outer loop then the button press is only registered once so I don't think it is the movedown function.
Simply keep track of the keystate in a variable. So you can reset a move counter. Something like this, combining both:
int MovingDown = 0;
...
if (!Key_Down(DIK_DOWN)) MovingDown = 0;
else if (MovingDown < 4) {
MovingDown++;
movedown(player);
}
Hard to tell without seeing more of your codebase but I would guess that you're not re-setting player.move after you've moved the player. Have you tried sticking some break poins in there to see what is being called and why?
If you remove the if(player.move == false) line does it have the same issue? ..or have you changed something elsewhere? If you could post more of your codebase like the whole movedown function and anythnig else which interacts (e.g. the animation code) it would help us help you...
[Edit] Is movedown a macro? Maybe you've got some peculiar side effect eminating from that? If not, it ought to have a semi colon after the function call.
What does KEY_DOWN return? Is that an API call? Try testing against the exact value you expect it to return?