Cocos2D Score Counter will not count score - cocos2d-iphone

I making a game in Cocos2D. I made a score counter on my screen, and when I hit an enemy it adds a point to the score. When I run it, and when I hit an enemy the project terminates.
It is probably because of this: Format specifies type 'id' but the argument has type 'int', referring to this line of code:
- (void)addPoint
{
score = score + 1; // score++; will also work.
[scoreLabel setString:[NSString stringWithFormat:#"%#", score]];
}
It says it wants me to replace the #"%#" to #"%d", because that will not make the score work, please help me fix this.
Thank you!

Yes, replace %# with %d. Score is integer, your question itself has answer..why simply asked here?
[scoreLabel setString:[NSString stringWithFormat:#"%d", score]];

Related

Trying to make a simple MUD like game but having issues with nested loops

So I'm a total noob so far and taking a course in JavaScript. I'm supposed to create a simple game with various choices using if, switch and while.
So I got my game going, but it won't recognize user input at some times, and I think it has to do with the way I've nested loops. The following code is inside first a switch and then a while loop. :
answer = prompt ("Välj SLÅSS eller PRATA?").toLowerCase();
if (answer ==="slåss")
{
console.log("Du närmar dig modigt besten");
alt = prompt("Använder du magi eller ditt vapen?").toLowerCase;
if (alt ==="magi") // this wont work, if I type in magi the code below won't trigger
{
console.log("Du kastar en eldboll som visar sig vara supereffektiv. Trollet dör.");
console.log ("Skattkistan var bakom trollet! Grattis! Du klarade spelet.");
skog=false;
break;
}
if(alt==="vapen") // this wont work, if I type in "vapen" the code below won't trigger
{
console.log("Du har inte en chans mot trollets styrka i närkamp och dör. Spelet är över.");
skog=false;
break;
}
else
{
console.log("Ange ett giltigt alternativ");
}
}
I also have an issue where I want a if statement to check if a earlier declared variable is true while also seeing if the user entered string is correct. I've writte this as follows :
else if (stark===true && answer === "prata")
//here I want you to loose if you choose this option and picked "prata" and "stark" earlier at the start.
you're missing () after toLowerCase:
alt = prompt("Använder du magi eller ditt vapen?").toLowerCase;
Your code was setting the variable to (lots of terminology here, sorry) a reference to the function toLowerCase instead of calling the function and getting its result.
Your second part of the question, about logical flow, seems like it can be solved by restructuring the code. There's an idea of "variable scoping" that will be helpful to you here. Since this is homework, I won't rewrite it for you, but see about declaring those variables as global with var at the beginning (as a first step for a beginner--I recognize that global variables are not great programming practice).

How to stop more than one being removed from an integer?

I know the title may seem fairly confusing, was just unsure on how to ask this...
So, I'm working on a basic sample game (not going to be a complete game or anything), where you can move around and are chased by an enemy character that attacks you. The problem is that when the attack function is called, instead of only removing one heart/hitpoint, they continue to be 'spam removed'. Here's what I'm working with...
void Enemy::attackPlayer()
{
if (distance < 50)
{
Player::health--;
return;
}
}
Pretty simple, right? Well the problem is that I need some way of I guess 'sleeping' the single function so that instead of continuing to remove health, it stops after one, then after let's say, 3 seconds, allows another attack to occur.
I think you can create two global time variables that are passed to your attack function. startTime is initiated once you call your attack function (outside). endTime is initiated right after removing one health from player (inside your function). Then you simply add a if statement before the distance if statement to check the delta time between these two and if they are more than 3 seconds then do the rest to remove another health.
You could probably have the Enemy class contain a method like:
bool canAttack(){
if(attackTimer >= 3000){
attackTimer = 0;
return true;
}
return false;
}
Then you could modify your damage condition to be something like:
if (distance < 50 && canAttack())
Of course, you would have to add a timer to the Enemy class and have it start and stop based on proximity to the player.
I'm sure there is a better way to handle this--also, this depends a bit on the implementation of the rest of your code. If you are using something like SFML, there is a built-in event system that would make this a bit easier to handle. Hopefully this helps a bit!
After taking some of the answers you guys gave me into consideration and messing around with some things by myself, I've came up with a pretty simple solution:
int Enemy::attackTime = 0;
And then...
void Enemy::attackPlayer()
{
if (distance > 60)
return;
if (time(0) > attackTime)
{
attackTime = time(0) + 3;
Player::health--;
}
}
I guess, player won't get another attacked from any enemy for 3 seconds. However, enemy can attack to another player if exist. Thus, this timer variable is keep into Player class. If I am correct, I think this code will work.
class Player
{
private:
uint32_t last_attack_timer;
...
public:
void set_last_attack_timer(uint32_t timer){this->last_attack_timer = timer;};
uint32_t get_last_attack_timer(void){return last_attack_timer;};
...
}
void Enemy::attackPlayer()
{
uint32_t timer = time(0);
if (distance < 50 && timer-Player::get_last_attack_timer>3000)
{
Player::health--;
Player::set_last_attack_timer(timer(0));
return;
}
}

Changing position in game is making problems

I tried to create table game with character and wanna to change position of my character through dice.
All points, where I wanna to get are saved in array global.array_points[1] to [20]: global.array_points[1]] = point_1;
When dice got number from 1 to 6 in step function of my character is:
if(character.x != global.array_points[global.new_complete_position].x &&
character.y != global.array_points[global.new_complete_position].y)
{
global.position_character += 1;
x = global.array_points[global.position_character].x;
y = global.array_points[global.position_character].y;
}
Sometimes my code is working perfect, but sometimes, my game still works, but character does not to know change position.
Do you know, how to solve this problem please?

C++ - Using specific character input for 'if' statements

I am, for lack of a better word, an absolute programming novice. And as it stands, I've been flung head first into programming something in two or three languages.
As part of an assignment, we have been tasked with essentially recreating a heads/tails flip, in the form of a random chance game.
The way it works is the player has ten credits to start off with. They are asked to input a wager, and then they are asked heads or tails. Picking either heads or tails sets off a number generator, and depending on the value, they may well end up winning or losing their wager.
I coded a similar version of this using HTML/CSS/JS, and it works. I'll leave a puush link to the file so you can view it yourselves to get an idea of what I'm trying to do: http://puu.sh/bP2V7/2ef63f4a1c.html
I'm trying to do, functionally, the same thing in C++ in the form of a command application. I know the code I'm using works fine, and it compiles without much of a hitch. It's a bit annoying that it closes down rather than resetting to a previous line, but that's a hurdle I'll jump over when I get to it.
I had a look around, and to be honest, whilst a few of the things may well work, I'm admittedly relatively clueless and some of the programmer speak kinda flies over my head.
It's probably because I'm being thrown into it and I'm not used to it yet, but as it stands, I need your help.
My code simply works as follows (simplified to save time):
int main()
{
int points, wager;
points = 10;
output "HEADS OR TAILS?";
if (player has 1 point or more)
{
output "Input wager";
input wager value;
output "Wager is (player input)";
output "Heads or Tails?";
input h or t; //This was what I wanted
if (player selects 'heads') //For sake of simplicity, the code
{ //here will account for both heads
int heads; //and tails.
srand(NULL);
heads = random number 1 and 2;
if (heads = 1)
{
output "HEADS!";
output "You win 'wager'!";
points = points + wager;
}
if (heads = 2)
{
output "TAILS!";
output "You lose 'wager'!";
points = points - wager;
}
}
}
if (player has 0 points)
{
output "GAME OVER";
}
}
What I want to do is have the user input either an 'h' or a 't' to determine whether or not they want heads or tails.
In your programming class, they will have told you what they expect you to use as tools for input and output, eg char inputChar; cin >> inputChar; or similar. Use whatever they told you to use in the style they want you to use, eg
cout << HEADS_OR_TAILS_PROMPT;
char inputChar;
cin >> inputChar;
switch(inputChar) {
case 'h':
{
... // code for the heads case
break;
}
case 't':
{
... // code for the tails case
break;
}
default:
// whatever you want to do if they didn't input a valid option
}
Although, to be honest, asking your professor is going to get you a better answer for what the grader is expecting than asking us is.

How to Store the highscore in the game

I am developing a game in xcode using cocos2d-x. I want to display the highscore in game over scene.when i run the game, score is displaying in the game over scene instead of highscore, even if the score is less than highscore. I think highscore is not stored. I am using the following code. Please help me to solve this.
CCUserDefault *def=CCUserDefault::sharedUserDefault();
long int high_score=0;
if(score>high_score)
{
def->setIntegerForKey(HIGH_SCORE, score);
//def->flush();
//high_score=def->getIntegerForKey(HIGH_SCORE);
}
else if(score<high_score)
{
def->setIntegerForKey(HIGH_SCORE, high_score);
//def->flush();
//high_score=def->getIntegerForKey(HIGH_SCORE);
}
high_score=def->getIntegerForKey(HIGH_SCORE);
char s[7];
sprintf(s,"%ld", high_score);
CCLabelTTF *high_label=CCLabelTTF::create(s, "Arial.fnt", 20);
high_label->setPosition(ccp(winwsize - 800, winhsize - 50));
this->addChild(high_label,2);
Make the high_core variable global.
Pay attention that if score<high_score nothing is really needed so you can drop the def->setIntegerForKey(HIHG_SCORE, high_score); statement.