GTA Enter/Exit car
#1
Posted 25 April 2012 - 09:50 AM
When I try to enter in the car with the Enter, it jumps to another position.
Here is the code:
Obj_player:
[Create event]
Global.incar = 0
[Keyboard pressed Enter]
if distance_to_object(obj_car) <= 64
{
visible=false
global.incar = 1
x = car.x
y = car.y
}
Obj_car:
[Step event]
if keyboard_check_pressed(vk_left)
{
if global.incar = 1
direction += 3
}
if keyboard_check_pressed(vk_right)
{
if global.incar = 1
direction -= 3
}
if keyboard_check_pressed(vk_up)
{
if global.incar = 1
speed = 5
}
if keyboard_check_pressed(vk_down)
{
if global.incar = 1
speed = -5
}
if keyboard_check_pressed(vk_enter)
{
if global.incar = 1
obj_player.visible = true
global.incar = 0
obj_player.x = x
obj_player.y = y
}
#2
Posted 25 April 2012 - 09:53 PM
When you press enter your car goes to the position of the player at the same time the player goes to the position of the car.Obj_player:
x = car.x
y = car.y
Obj_car:
obj_player.x = x
obj_player.y = y
Delete the lines that make the car go to the player and all should be fine
EDIT: Also, put the 'press enter' and what follows in the STEP event of the Obj_player, not the create event! The create event only executes codes once, when objects are created. The step event exectues code every step/frame.
And I think this should be in the 'Novice and Intermediate Users' board, since you're using GM8.
Edited by Wybo, 25 April 2012 - 09:58 PM.
#3
Posted 03 May 2012 - 01:07 PM
(obj_player):
[create]
global.incar=false[key enter event]
if distance_to_object(obj_car) <=100
{
if global.incar = false
{global.incar = true
instance_destroy()}
}
[obj_car][step]
image_angle = direction
if keyboard_check(vk_up)
{
if global.incar = true
speed = 5
}
if keyboard_check(vk_down)
{
if global.incar = true
speed = -5
}
if keyboard_check(vk_left)
{
if global.incar = true
direction += 3
}
if keyboard_check(vk_right)
{
if global.incar = true
direction -= 3
}
if keyboard_check(vk_enter)
{
if global.incar = true
global.incar = false
instance_create(x+30,y+30,obj_player)
}
#4
Posted 03 May 2012 - 10:17 PM
When I try to enter in the car with the Enter, it jumps to another position.
This is very brief. How am I meant to understand what you mean?
However.. and this is a guess, I think you mean that when you hit enter, instead of the player disappearing,
and the car being drivable, what happens is the player just 'jumps' to another x/y coord, correct?
The problem is with the enter key.
You press it, the player reads it, it sets global.incar = true, and destroys itself.
Now, the event passes on, and the car does HIS enter event.
It checks the key enter, and it is down, so he throws the player out again.
Back to square one!
First things first, use keyboard_check_pressed() instead of keyboard_check();
This function just checks for that 1 moment when the key is pressed (and not just if it is down!)
This 'may' solve your problem. If it doesn't, then you'll have to add a timer check. I'll alter some of your code so it looks like this:
if distance_to_object(obj_car) <=100
{
if global.incar = false
{global.incar = true
instance_destroy()
obj_car.alarm[0]=1;}
}
(assuming you only have 1 car object, and aren't using the [0] alarm!the car enter code:
if keyboard_check_pressed(vk_enter) and alarm[0]<=0
{
if global.incar = true
global.incar = false
instance_create(x+30,y+30,obj_player)
}
And in the car, create the alarm[0] event, and just add this code
///blank event. Using as timer.
You can, of course, use a variable, instead of the alarm. (In fact, it'd be best to... but hey, this works!)
I'll assume by your code that you only have 1 car object in the room?
(moving to novice Q&A.. that is where these sorts of questions belong.
#5
Posted 07 May 2012 - 06:16 PM
All right, I'll check that later.When I try to enter in the car with the Enter, it jumps to another position.
This is very brief. How am I meant to understand what you mean?
However.. and this is a guess, I think you mean that when you hit enter, instead of the player disappearing,
and the car being drivable, what happens is the player just 'jumps' to another x/y coord, correct?
The problem is with the enter key.
You press it, the player reads it, it sets global.incar = true, and destroys itself.
Now, the event passes on, and the car does HIS enter event.
It checks the key enter, and it is down, so he throws the player out again.
Back to square one!
First things first, use keyboard_check_pressed() instead of keyboard_check();
This function just checks for that 1 moment when the key is pressed (and not just if it is down!)
This 'may' solve your problem. If it doesn't, then you'll have to add a timer check. I'll alter some of your code so it looks like this:if distance_to_object(obj_car) <=100 { if global.incar = false {global.incar = true instance_destroy() obj_car.alarm[0]=1;} }(assuming you only have 1 car object, and aren't using the [0] alarm!
the car enter code:if keyboard_check_pressed(vk_enter) and alarm[0]<=0 { if global.incar = true global.incar = false instance_create(x+30,y+30,obj_player) }
And in the car, create the alarm[0] event, and just add this code///blank event. Using as timer.
You can, of course, use a variable, instead of the alarm. (In fact, it'd be best to... but hey, this works!)
I'll assume by your code that you only have 1 car object in the room?
(moving to novice Q&A.. that is where these sorts of questions belong.)
P.S:how to move the topic?
#6
Posted 07 May 2012 - 09:32 PM
If you want/need a topic moved (only if it's in the wrong forum! ) just hit the report button, and request one.
#7
Posted 09 May 2012 - 03:18 PM
Oh, I tried your method and it works perfectly.I already moved the topics, since I'm a forum moderator.
If you want/need a topic moved (only if it's in the wrong forum! ) just hit the report button, and request one.
Now just a little request, could you tell how do I make the player move to a car when enter is hitted.
Thanks
#8
Posted 09 May 2012 - 08:58 PM
If it is, then set it to true.
In a step event you'd be checking that variable. If it's true, then you start moving towards a car. (using something like move_towards_point(x,y,speed) or something similar)
#9
Posted 10 May 2012 - 05:17 PM
Ok I'll try this.you'd have a checker variable. When you hit enter, you'd check.. is that variable false?
If it is, then set it to true.
In a step event you'd be checking that variable. If it's true, then you start moving towards a car. (using something like move_towards_point(x,y,speed) or something similar)
#10
Posted 12 May 2012 - 09:53 AM
It didn't work, when I hit enter it steps towards the car, and then destriy it self, but the car doesn't move, and when I get out the car start moving.Ok I'll try this.
you'd have a checker variable. When you hit enter, you'd check.. is that variable false?
If it is, then set it to true.
In a step event you'd be checking that variable. If it's true, then you start moving towards a car. (using something like move_towards_point(x,y,speed) or something similar)
Could you give me the code please.
#11
Posted 12 May 2012 - 11:25 AM
#12
Posted 12 May 2012 - 12:30 PM
I then set it so that when the player is colliding with a vehicle of any kind, and they press the E key, it stores the colliding instance ID to a local variable called vehicle, the global.in_vehicle variable is set to true, and sets the player's visible attribute to false. If they are already in a vehicle, global.in_vehicle is set to false, vehicle is set to 0 (not (0), just 0) and visible is set to true.
Then, I have something in the step event that says that if global.in_vehicle is true, then x = vehicle.x and y = vehicle.y.
Then, in the player's A and D key events (left and right movement), I have it set so that if the player is NOT in a vehicle, his x is modified positively or negatively, based on which key you're pressing. Otherwise, if he IS in a vehicle, then with(vehicle), x is modified positively or negatively based on the key.
You might actually have to make it more specific if you have more than one vehicle and those vehicles all change sprites, or write something to account for sprite changing, due to the fact that you can't just write with(vehicle) { sprite_index = spr_car_right; } and expect it to not look weird when you enter a truck. However, if you're not changing sprites or you can figure something out for that, this should work really well.
EDIT: For sprite changing, you could probably do something in the actual car's step event sort of like:
if (x > xprevious) { sprite_index = spr_car_right; }
else if (x < xprevious) { sprite_index = spr_car_left; }
EDIT2: The best part about just making the player invisible (you can easily set collision checks to be ignored if the player is in a car) is that you don't have to worry about local variables being messed with every time you create/destroy the player object.
Edited by johnki, 12 May 2012 - 01:35 PM.
#13
Posted 12 May 2012 - 05:30 PM
This is not what I want the moderator understood my problem already.What I did in a recent prototype was have a global variable, like you have set up, called global.in_vehicle.
I then set it so that when the player is colliding with a vehicle of any kind, and they press the E key, it stores the colliding instance ID to a local variable called vehicle, the global.in_vehicle variable is set to true, and sets the player's visible attribute to false. If they are already in a vehicle, global.in_vehicle is set to false, vehicle is set to 0 (not (0), just 0) and visible is set to true.
Then, I have something in the step event that says that if global.in_vehicle is true, then x = vehicle.x and y = vehicle.y.
Then, in the player's A and D key events (left and right movement), I have it set so that if the player is NOT in a vehicle, his x is modified positively or negatively, based on which key you're pressing. Otherwise, if he IS in a vehicle, then with(vehicle), x is modified positively or negatively based on the key.
You might actually have to make it more specific if you have more than one vehicle and those vehicles all change sprites, or write something to account for sprite changing, due to the fact that you can't just write with(vehicle) { sprite_index = spr_car_right; } and expect it to not look weird when you enter a truck. However, if you're not changing sprites or you can figure something out for that, this should work really well.
EDIT: For sprite changing, you could probably do something in the actual car's step event sort of like:if (x > xprevious) { sprite_index = spr_car_right; } else if (x < xprevious) { sprite_index = spr_car_left; }
EDIT2: The best part about just making the player invisible (you can easily set collision checks to be ignored if the player is in a car) is that you don't have to worry about local variables being messed with every time you create/destroy the player object.
#14
Posted 15 May 2012 - 04:32 AM
see exactly what im talking about here
Edited by bob_gmae, 15 May 2012 - 04:40 AM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











