• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Problem when making a battle system.

R

ryan821

Guest
Hi,

I am a new GameMaker user and I am trying to make a project that simulates battle system like undertake. The concept is when I hit enter, the battle begins. Three circles will pop up and fly up.

This is the code for the circle:
Create:
Code:
flight_speed = 5;

dir = undefined;

alarm[0] = 1*room_speed;
Alarm[0]:
Code:
//0=LEFT 1=RIGHT 2=UP 3=DOWN
switch (dir) {
    case 0: x -= flight_speed; break;
    case 1: x += flight_speed; break;
    case 2: y -= flight_speed; break;
    case 3: y += flight_speed; break;
}

if (y <= -10) instance_destroy();

alarm[0] = 1;
And here's the code for a checkKey object:
Create:
Code:
global.battle = false;
Step:
Code:
if (!global.battle && keyboard_check_pressed(vk_enter)) {
    global.battle = true;
    instance_create_depth(x, y, 0, obj_Battle);
}
There's also a Battle object:
Create:
Code:
if (global.battle) {
    obj_Circle.dir = 2;
    instance_create_depth(96, 550, 0, obj_Circle);
    instance_create_depth(500, 620, 0, obj_Circle);
    instance_create_depth(900, 400, 0, obj_Circle);
    instance_destroy();
}
The room only has the checkKey object and a circle outside the room.

The problem I am having is the circle doesn't fly up. Please help! Also, how can I improve my code?

Thanks!
 

Attachments

Nidoking

Member
The reason your circle doesn't move is that you're setting the dir variable of the obj_Circle outside the room first, then creating three more obj_Circles and leaving their dir variables undefined. You need to create the circles first, then do something like
with (obj_Circle) dir = 2;
One way to improve your code is to use the Step Event rather than an alarm that's always reset to 1 for something you want to do every step. Also, most of what you've got there could be done with one object that calls a script. I don't understand the point of creating an instance that just does some stuff and destroys itself. There's also no need for a global variable to transfer information to it.
 
R

ryan821

Guest
The reason your circle doesn't move is that you're setting the dir variable of the obj_Circle outside the room first, then creating three more obj_Circles and leaving their dir variables undefined. You need to create the circles first, then do something like
with (obj_Circle) dir = 2;
One way to improve your code is to use the Step Event rather than an alarm that's always reset to 1 for something you want to do every step. Also, most of what you've got there could be done with one object that calls a script. I don't understand the point of creating an instance that just does some stuff and destroys itself. There's also no need for a global variable to transfer information to it.
It works! Thanks! <3
 
R

ryan821

Guest
The reason your circle doesn't move is that you're setting the dir variable of the obj_Circle outside the room first, then creating three more obj_Circles and leaving their dir variables undefined. You need to create the circles first, then do something like
with (obj_Circle) dir = 2;
One way to improve your code is to use the Step Event rather than an alarm that's always reset to 1 for something you want to do every step. Also, most of what you've got there could be done with one object that calls a script. I don't understand the point of creating an instance that just does some stuff and destroys itself. There's also no need for a global variable to transfer information to it.
Hi,

I got one more question. So, I want 5 circles to move up first then 5 more circles move left. But idk why only 5 circles move to the left and the 5 circles that suppose to move up doesn't show up.

GML:
if (global.battle) {
    instance_create_depth(384, 800, -y, obj_Circle);
    instance_create_depth(448, 800, -y, obj_Circle);
    instance_create_depth(512, 800, -y, obj_Circle);
    instance_create_depth(576, 800, -y, obj_Circle);
    instance_create_depth(640, 800, -y, obj_Circle);
    with (obj_Circle) dir = 2;
    instance_create_depth(-320, 512, -y, obj_Circle);
    instance_create_depth(-320, 448, -y, obj_Circle);
    instance_create_depth(-320, 384, -y, obj_Circle);
    instance_create_depth(-320, 320, -y, obj_Circle);
    instance_create_depth(-320, 256, -y, obj_Circle);
    with (obj_Circle) dir = 1;
}
Can you help me one more time? Thanks <3
 

chamaeleon

Member
Hi,

I got one more question. So, I want 5 circles to move up first then 5 more circles move left. But idk why only 5 circles move to the left and the 5 circles that suppose to move up doesn't show up.

GML:
if (global.battle) {
    instance_create_depth(384, 800, -y, obj_Circle);
    instance_create_depth(448, 800, -y, obj_Circle);
    instance_create_depth(512, 800, -y, obj_Circle);
    instance_create_depth(576, 800, -y, obj_Circle);
    instance_create_depth(640, 800, -y, obj_Circle);
    with (obj_Circle) dir = 2;
    instance_create_depth(-320, 512, -y, obj_Circle);
    instance_create_depth(-320, 448, -y, obj_Circle);
    instance_create_depth(-320, 384, -y, obj_Circle);
    instance_create_depth(-320, 320, -y, obj_Circle);
    instance_create_depth(-320, 256, -y, obj_Circle);
    with (obj_Circle) dir = 1;
}
Can you help me one more time? Thanks <3
Is your problem related to the fact that your with (obj_Circle) dir = 1; line will apply to all 10 instances, not just the instances created after the first with() statement, perhaps?
 

Nidoking

Member
Right. If you want to do different things with different circles, you'll need to store their ids (the return value of instance_create_depth) and use that to set the variables.
 
R

ryan821

Guest
Right. If you want to do different things with different circles, you'll need to store their ids (the return value of instance_create_depth) and use that to set the variables.
I think I did it overcomplicated but it works! Thanks <3
GML:
if (global.MAttack) {
    var _inst1 = instance_create_depth(384, 800, -y, obj_fire);
    var _inst2 = instance_create_depth(448, 800, -y, obj_fire);
    var _inst3 = instance_create_depth(512, 800, -y, obj_fire);
    var _inst4 = instance_create_depth(576, 800, -y, obj_fire);
    var _inst5 = instance_create_depth(640, 800, -y, obj_fire);
    with (_inst1) dir = 2;
    with (_inst2) dir = 2;
    with (_inst3) dir = 2;
    with (_inst4) dir = 2;
    with (_inst5) dir = 2;
    var _inst6 = instance_create_depth(-320, 512, -y, obj_fire);
    var _inst7 = instance_create_depth(-320, 448, -y, obj_fire);
    var _inst8 = instance_create_depth(-320, 384, -y, obj_fire);
    var _inst9 = instance_create_depth(-320, 320, -y, obj_fire);
    var _inst10 = instance_create_depth(-320, 256, -y, obj_fire);
    with (_inst6) dir = 1;
    with (_inst7) dir = 1;
    with (_inst8) dir = 1;
    with (_inst9) dir = 1;
    with (_inst10) dir = 1;
}
 

Nidoking

Member
In this case, you don't need to use with. You can just say _inst1.dir = 2; etc. It works either way, but I figured I'd let you know.
 

chamaeleon

Member
GML:
for (var i = 0; i < 5; i++) {
    var inst = instance_create(384+64*i, 800, -y, obj_fire);
    inst.dir = 2;
    inst = instance_create(-320, 256+64*i, -y, obj_fire);
    inst.dir = 1;
}
 
Top