I Wanna Community
Fangames => Game Design => Programming Questions => Topic started by: infern0man1 on June 19, 2015, 08:27:10 PM
-
(https://cdn.instructables.com/FDB/UQ9A/GB3W2DTV/FDBUQ9AGB3W2DTV.LARGE.jpg)
I'm attempting at making a shape similar to the one above, so I wrote up the code below:
//stepcounter coding beforehand and bla
xx=room_width/2;
yy=room_height/2;
dist=76;
num=4;
for(i=-num;i<=num;i+=1){
a=instance_create(xx+i*dist,yy+i*dist,cherry);
}
However, when I run the game, no cherry appears when it's supposed to. I'm thinking that it's just the way I wrote it that GM just doesn't even. Is there a better way or a workaround for this, or am I just completely missing something?
-
The way you were attempting to pull this off isn't exactly what I had in mind; each line in the pic of shape is straight, where the starting point of each line in the first quadrant would be {x+=1,y-=1}, and each quadrant is different. I just now realized that the code I wrote up would create cherries in the shape of a grid.
That method was also based off of a script I found:
//argument0=x
//argument1=y
//argument2=distance between 2 points
//argument3=number of points per half of each axis
var i,xx,yy,dist,num;
xx=argument0;
yy=argument1;
dist=argument2;
num=argument3;
for(i=-num;i<=num;i+=1){
draw_line(xx,yy+i*dist,xx+(num-abs(i))*dist,yy);
draw_line(xx,yy+i*dist,xx-(num-abs(i))*dist,yy);
}
I thought at first I could convert that into instance_create, but I failed to realize that this only takes into consideration the points on each axis.
I can easily create the axis, but not the lines of cherries going to different coordinates on the axis. How would you do that?
-
(https://s9.postimg.org/bq7sv3qf3/Untitled.png)
//stepcounter coding beforehand and bla
xx=room_width/2;
yy=room_height/2;
dist=50;
num=4;
for(i=-num;i<=num;i+=1){
for(j=-num;j<=num;j+=1){
xp = xx + (i *(abs(abs(j)-num)/num)) *dist;
yp = yy + (j *(abs(abs(i)-num)/num)) *dist;
a=instance_create(xp,yp,cherry);
}
}
-
I put something together that should be more accurate with larger shapes, gives you more control and should be useful for other things as well. I present to you draw_object_line(x1,x2,y1,y2, NumObjects, ObjectType)
put this in a script called draw_object_line
x1 = argument0;
y1 = argument1;
x2 = argument2;
y2 = argument3;
NumObjects = argument4;
Object = argument5
if NumObjects < 2 {NumObjects = 2;}
if x1 == x2
{
if y2 < y1
{
xt = x1;
yt = y1;
x1 = x2;
y1 = y2;
x2 = xt;
y2 = yt;
}
XStep = (x2 - x1)/(NumObjects-1);
YStep = (y2 - y1)/(NumObjects-1);
tx = x1;
for (ty = y1; ty <= y2; ty += YStep)
{
a = instance_create(tx,ty,Object);
tx += XStep;
}
} else {
if x2 < x1
{
xt = x1;
yt = y1;
x1 = x2;
y1 = y2;
x2 = xt;
y2 = yt;
}
XStep = (x2 - x1)/(NumObjects-1);
YStep = (y2 - y1)/(NumObjects-1);
ty = y1;
for (tx = x1; tx <= x2; tx += XStep)
{
a = instance_create(tx,ty,Object);
ty += YStep;
}
}
Now your shape drawing code looks pretty much the same as your starting code.
//stepcounter coding beforehand and bla
xx=room_width/2;
yy=room_height/2;
dist=50;
num=4;
for(i=-num;i<=num;i+=1){
draw_object_line(xx,yy+i*dist,xx+(num-abs(i))*dist,yy,5,cherry);
draw_object_line(xx,yy+i*dist,xx-(num-abs(i))*dist,yy,5,cherry);
}
This should look familiar
(https://s28.postimg.org/wi0v87pgd/Untitled3.png)
//stepcounter coding beforehand and bla
xx=room_width/2;
yy=room_height/2;
dist=25;
num=10;
for(i=-num;i<=num;i+=1){
draw_object_line(xx,yy+i*dist,xx+(num-abs(i))*dist,yy,num * 10,bullet);
draw_object_line(xx,yy+i*dist,xx-(num-abs(i))*dist,yy,num * 10,bullet);
}
-
Thank you so much my man <3
-
god damn it that looks nice
edit: whoopsied my other post. ._.
-
Wookie how do you even... You are a genius
-
Just found put that using a tile for the background image messed something up and hid the apples that were originally supposed to spawn OpieOP
-
Make sure the tiles have a higher depth value than the object running the code that is creating the fruit.
-
Do that with all the objects that might possibly overlap, the higher depth is, the further they are from the player (covered by objects with smaller depth)
-
Make sure the tiles have a higher depth value than the object running the code that is creating the fruit.
I did at the time; the tiles were at a depth of like 1000000 and the apples were at 0. IDK why they didn't show up since the rest of the attacks did, but w/e
-
Keep in mind that objects created in code will use the depth of the object that is running the code and not the depth set in the spawned object properties.