I Wanna Community

Fangames => Game Design => Programming Questions => Topic started by: LoneTree_tv on July 27, 2015, 12:31:38 AM

Title: Invincibility Frames
Post by: LoneTree_tv on July 27, 2015, 12:31:38 AM
I know how I could easily do invincibility frames(but describe how anyway, my way's probably wrong), but how do you make the boss toggle invisibility when it's invincible?

EDIT:

Also, how do I make an object rotate around another object?
Thanks in advance   :atkHappy:
Title: Re: Invincibility Frames
Post by: Kyir on July 27, 2015, 12:50:48 AM
You would create a timer (a variable in the creation code, a variable -= 1 in the step) and then set image_alpha = 0 until the timer runs out. If you already have the code for invincibility just stick the image_alpha 0 into that one. You can also make it image_alpha = .5 for half-visible, and so on.

To be more specific, I would make invincibility frames with the following

In the Create section:
IFrame = 0;
BossHP = 100 (or whatever value you want)

In the Collide with Bullet section:
if IFrame = 0 {
BossHP -= 1;
IFrame = 20}

In the Step section:
if IFrame > 0 {
IFrame -= 1;}

I'm pretty sure that should do what you want after you change the variables to your liking, but someone else might know better than me.
Title: Re: Invincibility Frames
Post by: LoneTree_tv on July 27, 2015, 01:03:42 AM
Thanks for the help.
There's just one thing:
when I said toggling, i meant it keeps on switching between visibility and invisibility even while invincible.
Do you know how to do that?
Title: Re: Invincibility Frames
Post by: LoneTree_tv on July 27, 2015, 01:13:40 AM
I got it working with the flashing by using an inconvenient way where
I have a bunch of lines saying

if (IFrame=x) {
image_alpha=x }

or something like that, but would it work if I triggered an alarm or something that says

if (image_alpha=0) {
image_alpha=1 }

if (image_alpha=1) {
image_alpha = 0 }

if (IFrame = 0) {
image_alpha = 1
}else{
alarm[11]=20

?
Title: Re: Invincibility Frames
Post by: LoneTree_tv on July 27, 2015, 01:25:09 AM
I got it working that way. Thanks for the help  :atkLove:
Title: Re: Invincibility Frames
Post by: infern0man1 on July 27, 2015, 01:54:10 AM
In the future, use the edit button instead of the post feature; it's against the rules to triple post unless each post is spread far enough away from each other in time or the content in it is too large to fit into one post (which really shouldn't happen either).
Title: Re: Invincibility Frames
Post by: LoneTree_tv on July 27, 2015, 06:04:43 PM
In the future, use the edit button instead of the post feature; it's against the rules to triple post unless each post is spread far enough away from each other in time or the content in it is too large to fit into one post (which really shouldn't happen either).

Sorry.  :atkCry:
I didn't want to post so many times,
but I forgot that editing existed.
Title: Re: Invincibility Frames
Post by: Derf on July 27, 2015, 06:12:44 PM
I'm not sure it makes much of a difference to the speed of the code but it's probably better to use the in-built function for visibility than image_alpha since you're just using 0 & 1 anyway. e.g. "visible = 1" or "visible = 0".

Also when you're just using boolean data (a value that is either 1 or 0/true or false) you can just do the following:

Code: [Select]
visible = !visible;
instead of

Code: [Select]
if (visible = 0) {
     visible = 1;
}
else if (visible = 1) {
     visible = 0;
}

Again, it's not a big enough thing to cause slowdown in the game but it is quicker for GM to process and is a good habit simply because it requires you to type less.
Title: Re: Invincibility Frames
Post by: HAEGOE on July 27, 2015, 09:12:49 PM
Well, I'm using this code to toggle a couple of frames later

Code: [Select]
if IFrame>0 and IFrame mod 4 <2{
//the former is to make sure invisibility toggling works only when invincible, and the latter is for setting toggle interval
visible = 0;
}else{//when the boss is not invincible or when toggled from invisible to visible
visible = 1;
}

in the code above, I setted the interval to 2, means boss' invisibility changes each 2 frames.

If you want to increase it(decreasing would means setting it to 1, in which case It's better to use what derf said), you can write it this way

Code: [Select]
if IFrame>0 and IFrame mod (interval X2) <(interval){
//same as above. I suggest writing value as natural number
visible = 0;
}else{
visible = 1;
}
Title: Re: Invincibility Frames
Post by: LoneTree_tv on July 27, 2015, 11:13:28 PM
Wow. Thanks so much!
But one more thing, can you break down the first lines of code for both of the segments?
I don't understand "mod 4 <2" and "mod (interval X2) <(interval)" very well.
Just for future usage, I'd like to know what they do.
Thanks in advance  :atkHappy:
Title: Re: Invincibility Frames
Post by: HAEGOE on July 28, 2015, 04:38:30 AM
first, here 'mod' means 'modulo operation' or 'remainder'.

so, if you say IFrame mod 4 < 2, it means if you divide 4 into the value 'IFrame' and you get a remainder less than 2.

and since the IFrame is dropping every frame, the remainder of the calculation will also be changed.
as the IFrame goes down, the remainder(IFrame mod 4) will take a cycle of 3->2->1->0->3->2->1->0->3->.. and so on.
now, when remainder is 1 or 0, then the IFrame mod 4 < 2 will be true, which means visible = 0 and boss will be invisible.
and when remainder is 2 or 3, then it will be false, visible = 1 and boss will be visible.

same goes with mod (interval X2) <(interval).
As the IFrame drops, IFrame mod (interval X2), or remainder will take a cycle from interval X2-1 to zero, then back to interval X2-1.
and if the remainder is smaller than interval, then the code will be true, and I believe you'll get the picture.

Of course, interval X2 means twice the interval. By setting it that way, half the remainder will be same or higher than the interval(which means false), and the other half will be lower than that(true), so that you can control the number of frames of invisibility toggle.

hope this is simple enough.. I'm not good at explaining things  :atkCry:
Title: Re: Invincibility Frames
Post by: LoneTree_tv on July 28, 2015, 08:23:26 PM
Alright, thanks. I understood most of it, except for the interval and interval X2 stuff.
No need to explain. But if someone does know how to explain it to an absolute beginner like me, please do.

In the case that I do use the interval code, and just copy paste it,
what do I need to change to make the interval longer or shorter, and how many frames longer will it make it?
Title: Re: Invincibility Frames
Post by: HAEGOE on July 29, 2015, 06:50:14 AM
uhh.. you see, that's where the interval and interval X2 comes in.

for example, if you want to set interval as 5 frame, do this :

if IFrame>0 and IFrame mod 10 < 5 {
//the former is to make sure invisibility toggling works only when invincible, and the latter is for setting toggle interval
visible = 0;
}else{//when the boss is not invincible or when toggled from invisible to visible
visible = 1;
}
basically the same, but there interval is 5 and interval X2 is 10.
Title: Re: Invincibility Frames
Post by: LoneTree_tv on July 29, 2015, 04:41:00 PM
So interval isn't a constant or any thing, it'd just the number of frames that it takes before toggling?
Oops.
I thought it was some sort of built in function to gm.
Also, if I wanted the boss to stay visible twice as long as it stays invisible, I could put something like
mod 8 < 3
right?
finally, to clear up some confusion, MY idea wag up at the top of the thread that I said worked was in a form of an alarm. It didn't flash at every frame. I put alarm [11]=20 at the end of it.
sorry if my grammar or something sucks, I wrote this all on my phone :P
Title: Re: Invincibility Frames
Post by: LoneTree_tv on August 02, 2015, 11:40:25 PM
One more thing, hopefully the last,
How do you make a health bar appear?
Title: Re: Invincibility Frames
Post by: lawatson on August 03, 2015, 08:16:35 AM
Make a sprite for the health bar which is 1 pixel, and then draw it with the length of the maximum length / max HP * HP.
Title: Re: Invincibility Frames
Post by: LoneTree_tv on August 03, 2015, 12:40:17 PM
How do you change the length of the sprite?
Title: Re: Invincibility Frames
Post by: lawatson on August 03, 2015, 12:45:50 PM
image_xscale = x

it's multiplicative, so if you choose 1, it'll show the sprite, if you choose 2, it'll double the length, -1 will reverse the sprite, etc.

EDIT: Or actually, you'd probably rather just wanna put in the draw event:

draw_sprite_ext(spr_healthbar,0,x,y,image_xscale,1,0,c_white,1)

Replace the variables with whatever.
Title: Re: Invincibility Frames
Post by: LoneTree_tv on August 03, 2015, 12:52:16 PM
Alright, I tried using the draw event, which I've never used before, and ran into some problems

I only wrote this:

draw_sprite(HPBar,0,10,10)

and neither the sprite nor the boss appears.

also, just now, i tried

hpbar=draw_sprite(HPBar,0,10,10);
hpbar.image_xscale=500/20*hp

the boss and hp bar did not show, and the players sprite got stretched instead.

EDIT: Oops, didn't see your edit. Tried it, and it still didn't work. what should I put for the image scaling if the max hp is 20 and I want the length to be 500? I think the fact that I put "500/20*hp" is what screwed everything up.

EDIT2: Wow, I'm incredibly stupid. I replaced spr_healthbar with my sprite name, and that fixed it. But the boss still doesn't seem to want to exist. By that I mean it's invisible. It's still shootable if you can guess where he is.

EDIT3: Once again, I'm terrible. I just put in draw_self(); before the other line and everything worked perfectly.
Title: Re: Invincibility Frames
Post by: lawatson on August 03, 2015, 02:04:29 PM
Yeah, maybe I should have also said that you should put in draw_self() when using the draw event, but otherwise, there you go!
Title: Re: Invincibility Frames
Post by: LoneTree_tv on August 03, 2015, 03:19:25 PM
Thanks  :atkHappy:

I also wanted to know how to make cherries rotate around the boss, but I scratched the idea as a whole. It'd still be nice to know how, if you know.
Title: Re: Invincibility Frames
Post by: Katz on August 03, 2015, 03:22:41 PM
Thanks  :atkHappy:

I also wanted to know how to make cherries rotate around the boss, but I scratched the idea as a whole. It'd still be nice to know how, if you know.

I would do either paths or move_towards_point code(s) with a 360 rotation

I guess the first one works better if math is hard :P
Title: Re: Invincibility Frames
Post by: LoneTree_tv on August 03, 2015, 03:24:51 PM
Math is ezpz, and I don't know how to use paths :P

But how would that work in code?

with deliciousFruit {
move_towards_point(boss.x,boss.y,90) }

?
Title: Re: Invincibility Frames
Post by: Derf on August 03, 2015, 03:57:27 PM
off the top of my head something like this

make a special fruit object:

Code: [Select]
CREATE EVENT:
cx = boss.x
cy = boss.y
rad = 12; //or whatever you want the radius to be
spd = 8;//or whatever you want the rotational speed to be
ang = 90;//or whatever you want the starting ang of the object to be

Code: [Select]
STEP EVENT:
ang = ang mod 360;//this makes it so that the ang is always between 0 & 360

if(ang < 0) {
     ang += 360;
}

x = cx + rad * cos(degtorad(ang)); //make sure cherry is at correct point in rotation
y = cy - rad * sin(degtorad(ang));

ang += spd; //rotate cherry

cx = boss.x;
cy = boss.y; //update the centre points so that if your boss is moving, the cherries move with it
Title: Re: Invincibility Frames
Post by: LoneTree_tv on August 03, 2015, 04:01:22 PM
That looks incredibly complicated.
Also, you didn't use the move_towards_point thing, telling me there's probably a different method? I'd like to know just in case. Thanks :atkHappy:
Title: Re: Invincibility Frames
Post by: Katz on August 03, 2015, 04:11:52 PM
I 100% recommend derf's method in this instance. move_towards_point would just be simple and shitty imo.
Title: Re: Invincibility Frames
Post by: LoneTree_tv on August 03, 2015, 04:12:29 PM
alright then. Thanks  :atkHappy:
Title: Re: Invincibility Frames
Post by: Derf on August 03, 2015, 04:19:30 PM
You only really need to mess with the create event variables to tweak it to fit your game so it shouldn't matter if you don't understand it 100%.

Generally there are more than one way of doing most things in Game Maker and it's just up to personal preference and optimization really.
Title: Re: Invincibility Frames
Post by: infern0man1 on August 03, 2015, 04:49:28 PM
A while back sand wrote this script for orbiting, I hope he won't mind I put it here:


Code: (orbit) [Select]
argument0=centerX; //the center x-coordinate for the orbit
argument1=centerY; //the center y-coordinate for the orbit
argument2=radius;
argument3=spinSpeed; //the speed the projectile is moving in the orbit
argument4=direction;
argument5=radiusSpeed; //the speed the projectile is moving along the radius (a speed of -1 would make the projectile move inwards)



x=centerX+lengthdir_x(radius+rad,direction+dir);
y=centerY+lengthdir_y(radius+rad,direction+dir);
dir+=spinSpeed;
rad+=radiusSpeed;


And in the projectile's step event:


Code: [Select]
orbit(centerX,centerY,radius,spinSpeed,direction,radiusSpeed);


You could set the defaults in the projectile's step event, but for me, when I set the spinSpeed in the step event, the projectiles will just speed up until it hits a certain speed, then slow back down, as if it were a sine wave. To avoid this, I usually just put in either a spawning object's alarm[_] event or the boss's alarm[_] event:


Code: [Select]
for(i=0;i<360;i+=360/[number]){
  a=instance_create([x value],[y value],cherry);
  a.centerX=a.xstart;
  a.centerY=a.ystart;
  a.radius=[number];
  a.spinSpeed=[number];
  a.direction=i;
  a.radiusSpeed=[number];
}


If you don't want the projectiles to move in towards the orbit's center, you can remove rad and radiusSpeed from each code or just set it radiusSpeed to 0.