# Creating a math platformer, need help with arrays

Get help using Construct 2

### » Mon May 21, 2012 3:57 am

Hi all! I had one thread previously, and you were all incredibly helpful with that one, so I've returned for more excellent advice.

I searched through the tutorials in the sticky and on the main website and although there is an Arrays tutorial, I'm not exactly sure that arrays are what I need. (they seem a little bit verbose for what I need...?)

What I need:

I'm creating a math platformer, and there will be an event where a player has to solve a math problem to proceed, and I am wondering what the best way to generate problems would be? Is there some way to procedurally/randomly create them? Should I create a list and then pull one from the list? How will C2 know if the player has entered the correct answer (with the numbers on the keyboard)?

I know this probably sounds pretty basic to some of you, but I am new to algorithmic reasoning and these problems kind of stump me.
B
5
Posts: 18
Reputation: 461

### » Mon May 21, 2012 7:54 am

I think the solution really depends on the complexity of the math problems you have in mind. Will they be very simple addition or subtraction problems, perhaps for children?

If they're advanced math problems, you may need to get into some lightweight math parsing or perhaps rely on JavaScript's eval function via the CallJS plugin (it's probably safer to use an external JavaScript parser like the this one).

For simpler math problems, you can probably just create 2 random numbers and randomize an operator with something like the following:

x = int(random(1)) + 10
y = int(random(1)) + 10
operate = choose("+", "-", "/", "*")

MathString = str(x) & operate & str(y)

You can store a series of such strings within your array, then when you choose a random element out of the array, solve it using a special event sheet.

That'd be my own idea for a solution. Perhaps others may have better ideas.
B
126
S
7
G
4
Posts: 185
Reputation: 7,233

### » Mon May 21, 2012 4:59 pm

[QUOTE=Ugotsta]

You can store a series of such strings within your array, then when you choose a random element out of the array, solve it using a special event sheet.
[/QUOTE]

Yes, it's math for children in 4th grade, so this sounds good. Could you explain how you would go about creating a solution event sheet? So if the prompt ends up being

12 * 2 = ?

How does the user input the answer? Sorry if this is all very basic stuff.
B
5
Posts: 18
Reputation: 461

### » Mon May 21, 2012 6:57 pm

well since its a platformer game why not make the player input the answer by jumpin on the platform with the right answer? or jump on a 10 platform twice and a 1 platform 4 times to get 24?

Or you could always just take the simple approach and make a text input box appear and they type it
B
13
S
5
G
2
Posts: 306
Reputation: 3,262

### » Mon May 21, 2012 9:57 pm

[QUOTE=aridale] well since its a platformer game why not make the player input the answer by jumpin on the platform with the right answer? or jump on a 10 platform twice and a 1 platform 4 times to get 24?

Or you could always just take the simple approach and make a text input box appear and they type it[/QUOTE]

The problem with using the text input box is: The game is locally multiplayer, and only one player will have to enter in numbers... The other player needs to be able to input controls while the numbers are being entered.
B
5
Posts: 18
Reputation: 461

### » Mon May 21, 2012 10:44 pm

Here's an idea using an array to store numbers entered on the num-pad:
mathPlatformer.capx (r91)
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,860

### » Mon May 21, 2012 11:47 pm

[QUOTE=ramones] Here's an idea using an array to store numbers entered on the num-pad:
mathPlatformer.capx (r91)[/QUOTE]

That's just about perfect. Thank you so much. What is the difference between push back and push front? I am imagining the array as a line of blocks, and push back means adding elements (blocks) from the right side, and push front is adding elements (blocks) from the left side. Is that correct?

Edit: Man, this just makes sense. I freaking love C2. nihakue2012-05-22 00:01:43
B
5
Posts: 18
Reputation: 461

### » Tue May 22, 2012 12:37 am

So here's the capx with ramone's code implemented. (use wasd to control the green block, but the camera follows the little dude, so use arrow keys to move him around)

The orange block is the "challenge"

http://dl.dropbox.com/u/11790413/Final_Project.capx

Do 'or' blocks not work with key presses? so I could do :

-'on 1 or num 1 pressed'

Or should I just create a separate event?
B
5
Posts: 18
Reputation: 461

### » Tue May 22, 2012 2:44 pm

Yeah you can use 'or' with the keyboard triggers. BTW an easier way to reset the array, instead of the loop I used, is:
input->Set size to (0,1,1)
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,860