• 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!

Need help with knockback in certain direction!

D

Doooooli

Guest
I have a simple spell, which when it collides with a certain block it pushes the block in a direction 10 pixels!


So this is what I have so far
Code:
other.y -= 10;
In theory everything works, but that isn't exactly how I wanted it! You see I want the block to be pushed in the direction that the player is looking to! So if I hit it from the right it should go right, and vice versa on the left, and if I hit it from the bottom it should go up and vice versa for up! Which I believe isn't too hard to achieve, but what I tried did not work and produced an error! What I tried was,
Code:
other.obj_player.knockbackspell.direction -= 10;
but to no success! So if anyone would like to tell me how to fix it I would be pleased :)

thx!
 
Last edited by a moderator:
F

Freedom2Fight

Guest
How about having a collision event in the movable block. Specifically a collision event with the spell projectile.

In it say write something like:

is_hit = true;
push_back = 10

In the step event:

y += spell_player_direction * push_back

Something like that.

Edit: Oh. Right. Its going to keep on moving unless is_hit is set to false.
 

Roderick

Member
Assuming that this is in the player's collision with object code:

Code:
var move_dir = point_direction(x, y, other.x, other.y);
other.x += lengthdir_x(10, move_dir);
other.y += lengthdir_y(10, move_dir);
This will almost always result in sub-pixel movement. If you want to avoid that, wrap the lengthdir_* commands in round() commands.
 
X

Xskode

Guest
I have a simple spell, which when it collides with a certain block it pushes the block in a direction 10 pixels!


So this is what I have so far
Code:
other.y -= 10;
In theory everything works, but that isn't exactly how I wanted it! You see I want the block to be pushed in the direction that the player is looking to! So if I hit it from the right it should go right, and vice versa on the left, and if I hit it from the bottom it should go up and vice versa for up! Which I believe isn't too hard to achieve, but what I tried did not work and produced an error! What I tried was,
Code:
other.obj_player.knockbackspell.direction -= 10;
but to no success! So if anyone would like to tell me how to fix it I would be pleased :)

thx!
Im assuming you're using X and Y for movement instead of h&v speed.

there are a few ways of doing this. For I have do this in one of my projects called Maze Attack.

I don't know how your code is set up so I give an example that should be easy to use without changing much code.

1st way:
on the collision event of the spell (when spell hits block) you can check location which sprite is being used

(of course you'll have to change the names to the correct ones)
now there was some confusion cause you say if the player hits it from the right it'll go right- but then
you say you want it to go the way the player is facing- which would mean the player being on right would make block go left since player would be facing that direction.
nevertheless, I think I understand what you're looking for.

Collision Spell with block:
Code:
var player_x = obj_player.x;

if x < player_x
   {///player is on right side
      x += 10; ///move towards player
   }
else if x > player_x
   {////player is on left side of block
      x -= 10; ///move towards player
   }
that should make the block move in the direction the player is standing on: right - moves right and vice-versa

let me know if that works :)
 
D

Doooooli

Guest
The collision is inside the spells collision event with the block :) but I'll try out your examples :) thanks
 
D

Doooooli

Guest
As long as the spell is an object, my code should work just fine for you.
it sort of works :) the block moves in the way I'm facing, but, it also moves to another direction :p so for instance if I stand below it it moves up and to the left, and if I stand over it it moves down and to the right :)
 

Roderick

Member
it sort of works :) the block moves in the way I'm facing, but, it also moves to another direction :p so for instance if I stand below it it moves up and to the left, and if I stand over it it moves down and to the right :)
That sounds like you used one lengthdir_* command in both lines. Double check that you're using lengthdir_x and lengthdir_y, as approximate.
 
D

Doooooli

Guest
That sounds like you used one lengthdir_* command in both lines. Double check that you're using lengthdir_x and lengthdir_y, as approximate.
Not to sound mean or dumb, but I'm clueless, how do you mean? :)
 
X

Xskode

Guest
dude, if you want just save your code as a file (through game maker) then just post is here.
Cause what you're asking for is the easiest thing to do in game maker, but since you don't provide any code or screenshots, or anything other than 'here is my issue'
you're making it harder for us to fix (probably) the easiest issue we ever help someone with.

not trying to be rude, just being real. if you want this problem fixed we're gonna need enough info about the problem.
for example. we don't know if maybe you have a parent on an object thats causing this issue, maybe it's a variable that isn't receiving the information correctly (in correct passing of value)
we don't even know where your code is. like in begin step, create, alarm, etc...

so if you want to be helped, then you have to help us with enough information. cause there are SO MANY very very very skilled programmers and 'bug smashers' within the yoyogame community
that want to help others. but these people are not magic- meaning, we need enough information to solve the problem or help you get to a solution.

you gave us a few lines of code, but where is the code at. cause in game maker putting code in the wrong event can cause issues as well.
 
D

Doooooli

Guest
dude, if you want just save your code as a file (through game maker) then just post is here.
Cause what you're asking for is the easiest thing to do in game maker, but since you don't provide any code or screenshots, or anything other than 'here is my issue'
you're making it harder for us to fix (probably) the easiest issue we ever help someone with.

not trying to be rude, just being real. if you want this problem fixed we're gonna need enough info about the problem.
for example. we don't know if maybe you have a parent on an object thats causing this issue, maybe it's a variable that isn't receiving the information correctly (in correct passing of value)
we don't even know where your code is. like in begin step, create, alarm, etc...

so if you want to be helped, then you have to help us with enough information. cause there are SO MANY very very very skilled programmers and 'bug smashers' within the yoyogame community
that want to help others. but these people are not magic- meaning, we need enough information to solve the problem or help you get to a solution.

you gave us a few lines of code, but where is the code at. cause in game maker putting code in the wrong event can cause issues as well.
Dude read again please :) the line that I wrote is the full code I use :p nothing more
 
X

Xskode

Guest
okay. so if that is all you used. then my apology for saying you didn't provide everything for the code.

however, it is still a little uncertain.

so the code i'll provide will be off the assumption that the spell hits block and in the collision event of the spell that is where you placed the other.y -= 10.

as for the second line of code ( other.obj_player.knockbackspell.direction -= 10; ) im not sure what event that code is in- or why it's there. this can all be done from the collision event (and one line of code in the create event) of the spell object.

the following code is starting from no code in collision event and adding two lines of code in the create event with spell object.


Code:
Create Event:
px = player.x;
py = player.y;

Collision Event With Block:
/*
the following will check which direction the block is being hit from, then will verify the direction based on the player's X and/or Y location when the spell was used.
(keep in mind you can do this SO MANY WAYS this is just the first way that came to my mind, but you can use collision_rectangle, circle, distance_to_point, etc...)
*/

///checking direction spell from right side
if other.x < x
{///verify player's location
   if other.x < px
   {///player used spell from right side of block and it hit
      other.x += 10;
      instance_destory(); ///destroys spell
   }

}
///checking direction spell from left side
else if other.x > x
{///verify player's location
   if other.x > px
   {///player used spell from left side of block and it hit
      other.x -= 10;
      instance_destroy(); ///destroys spell
   }

}

///checking direction spell from above
else if other.y < y
{///Verify player's location
   if other.y < py
   {///player used spell from above and it hit
      other.y -= 10;
      instance_destroy(); ///destroys spell
   }
}

///checking direction spell from below
else if other.y > y
{///Verify player's location
   if other.y > py
   {///player used spell from below
      other.y += 10;
      instance_destroy(); ///destroys spell
   }
}

that code will move the block in the direction the player is facing- more like in the direction the player is standing.
if you want it to move in the direction the player is facing you can check player's sprite direction or you can use a logical understanding- meaning if someone was on the right of something and hit something on the left side then they would have to be facing left at that moment. ( this makes the code easier to make instead of checking the sprite direction.

Code:
Create Event:
px = player.x;
py = player.y;

Collision Event With Block:
/*
the following will check which direction the block is being hit from, then will verify the direction based on the player's X and/or Y location when the spell was used.
(keep in mind you can do this SO MANY WAYS this is just the first way that came to my mind, but you can use collision_rectangle, circle, distance_to_point, etc...)
*/

///checking direction spell from right side
if other.x < x
{///verify player's location
   if other.x < px
   {///player used spell from right side of block and it hit
      other.x -= 10;
      instance_destory(); ///destroys spell
   }

}
///checking direction spell from left side
else if other.x > x
{///verify player's location
   if other.x > px
   {///player used spell from left side of block and it hit
      other.x += 10;
      instance_destroy(); ///destroys spell
   }

}

///checking direction spell from above
else if other.y < y
{///Verify player's location
   if other.y < py
   {///player used spell from above and it hit
      other.y += 10;
      instance_destroy(); ///destroys spell
   }
}

///checking direction spell from below
else if other.y > y
{///Verify player's location
   if other.y > py
   {///player used spell from below
      other.y -= 10;
      instance_destroy(); ///destroys spell
   }
}
As you can see it's the same code, just the direction change from moving towards player to moving away from player.

let me know if this works for you.
 
Top