Login | Register

Sword collision

July 29, 2006, 12:03 by Calle
[loading]
-->
This is an extension to the original article about arm-sword connection. I remember myself writing that it is tricky because the sword itself isn’t an object of its own. It’s not that tricky actually, and this is the only code:

<div style="clear: both;">dir = point_direction(x+150,y+90,mouse_x,mouse_y);

inst = collision_line(x+150,y+90,x+150+lengthdir_x(175,dir),
y+90+lengthdir_y(175,dir),enemy,false,true);
if (inst>0) {
with (inst) {
instance_create(x,y,ded_enemy);
instance_destroy();
}
}
</div>

To try this out I need two objects. There is a reason to why I can’t have just one (obj_enemy). You will understand that reason once you have understood the code. What I do is that I set a variable the return value of function collision_line. That line is set with the same value as I used to draw the sword. The problem which arises with this method is that collision_line only return the ID of the first hit specified object. If I had written inst.image_single = 1 that would have the same effect (because that’s the only change in ded_enemy) but not totally. Because I couldn’t be able to kill an enemy as long as there was another enemy in front. Normally this is not what you want.

Download the example and see the changes yourself:
http://gmtutorials.com/files/armsrotcoll.gm6

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games