Jump to content


Photo

GTA Enter/Exit car


  • Please log in to reply
13 replies to this topic

#1 cj sniper 98

cj sniper 98

    GMC Member

  • GMC Member
  • 96 posts
  • Version:GM8

Posted 25 April 2012 - 09:50 AM

Hi guys, I'm making a gta game, I added a car, the problem is:
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
}
  • 0

#2 Wybo

Wybo

    GMC Member

  • GMC Member
  • 3 posts

Posted 25 April 2012 - 09:53 PM

Obj_player:
x = car.x
y = car.y

Obj_car:
obj_player.x = x
obj_player.y = y

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.
Delete the lines that make the car go to the player and all should be fine :thumbsup: .

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.

  • 0

#3 cj sniper 98

cj sniper 98

    GMC Member

  • GMC Member
  • 96 posts
  • Version:GM8

Posted 03 May 2012 - 01:07 PM

look, I've mde complety different code, and the same problem again:
(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)
}

  • 0

#4 Desert Dog

Desert Dog

    GMC Member

  • Retired Staff
  • 6409 posts
  • Version:Unknown

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? :tongue:

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. :thumbsup: )
  • 0

#5 cj sniper 98

cj sniper 98

    GMC Member

  • GMC Member
  • 96 posts
  • Version:GM8

Posted 07 May 2012 - 06:16 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? :tongue:

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. :thumbsup: )

All right, I'll check that later.
P.S:how to move the topic?
  • 0

#6 Desert Dog

Desert Dog

    GMC Member

  • Retired Staff
  • 6409 posts
  • Version:Unknown

Posted 07 May 2012 - 09:32 PM

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.
  • 0

#7 cj sniper 98

cj sniper 98

    GMC Member

  • GMC Member
  • 96 posts
  • Version:GM8

Posted 09 May 2012 - 03:18 PM

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.

Oh, I tried your method and it works perfectly.
Now just a little request, could you tell how do I make the player move to a car when enter is hitted.
Thanks
  • 0

#8 Desert Dog

Desert Dog

    GMC Member

  • Retired Staff
  • 6409 posts
  • Version:Unknown

Posted 09 May 2012 - 08:58 PM

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)
  • 0

#9 cj sniper 98

cj sniper 98

    GMC Member

  • GMC Member
  • 96 posts
  • Version:GM8

Posted 10 May 2012 - 05:17 PM

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)

Ok I'll try this.
  • 0

#10 cj sniper 98

cj sniper 98

    GMC Member

  • GMC Member
  • 96 posts
  • Version:GM8

Posted 12 May 2012 - 09:53 AM


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)

Ok I'll try this.

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.
Could you give me the code please.
  • 0

#11 Desert Dog

Desert Dog

    GMC Member

  • Retired Staff
  • 6409 posts
  • Version:Unknown

Posted 12 May 2012 - 11:25 AM

Just make it so that you can move the car when the player is gone..?
  • 0

#12 johnki

johnki

    GMC Member

  • GMC Member
  • 519 posts
  • Version:GM:Studio

Posted 12 May 2012 - 12:30 PM

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.

Edited by johnki, 12 May 2012 - 01:35 PM.

  • 0

#13 cj sniper 98

cj sniper 98

    GMC Member

  • GMC Member
  • 96 posts
  • Version:GM8

Posted 12 May 2012 - 05:30 PM

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.

This is not what I want the moderator understood my problem already.
  • 0

#14 bob_gmae

bob_gmae

    GMC Member

  • GMC Member
  • 22 posts
  • Version:GM8

Posted 15 May 2012 - 04:32 AM

i have a player object that jumps to the position of the car object then destroys the car then changes its sprite and controls to a car's. when the car turns at and angle, i want the player to jump to the side of the door instead of the top or sides of the car. how would i do that? this is direction based.
see exactly what im talking about here Posted Image


Posted Image

Edited by bob_gmae, 15 May 2012 - 04:40 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users