Fangames > Programming Questions

Problems with creating this shape

(1/3) > >>

infern0man1:


I'm attempting at making a shape similar to the one above, so I wrote up the code below:


--- Code: (objBoss - Step Event) ---//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);
}

--- End code ---

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?

infern0man1:
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:


--- Code: ---//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);
}

--- End code ---

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?

WetWookie:


//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);
    }
}

WetWookie:
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

--- Code: ---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;
    }   

}

--- End code ---

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


//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);
}

infern0man1:
Thank you so much my man <3

Navigation

[0] Message Index

[#] Next page

Go to full version