Discuss Scratch

Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

I'm making Super Mario RPG and I'm making the 8 different ways you can walk, but for the front, it's glitching! By the way, here's the link to the game: https://scratch.mit.edu/projects/1010275185/
deck26
Scratcher
1000+ posts

My Mario moveset is glitchy! D:

If down and left are pressed both scripts are running. You should check for down arrow pressed and then within that if block check whether left is pressed or not (if/else) choosing one broadcast or the other accordingly.

You should probably also use broadcast and wait or have a wait until keys not pressed to prevent the broadcast receiver being constantly restarted - you're broadcasting again each time the forever loop loops.
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

I'm not that good at coding, could you make a remix of it so i can use it?
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

deck26 wrote:

If down and left are pressed both scripts are running. You should check for down arrow pressed and then within that if block check whether left is pressed or not (if/else) choosing one broadcast or the other accordingly.

You should probably also use broadcast and wait or have a wait until keys not pressed to prevent the broadcast receiver being constantly restarted - you're broadcasting again each time the forever loop loops.

Could you make a remix of the game fixing this bug?
deck26
Scratcher
1000+ posts

My Mario moveset is glitchy! D:

Leandro_2013 wrote:

I'm not that good at coding, could you make a remix of it so i can use it?
It's not hard. You want something like

forever
if <key [down v] pressed?> then // you'll want your letter key as well, I'm leaving that to you
if <key [left v] pressed?> then // you'll want your letter key as well, I'm leaving that to you
broadcast [left-and-down v]
else
broadcast [down-only v]
end
wait until <not <key [any v] pressed?>>
end
end
But if you also want to detect down and right you need to include the right key in the above - if you have another copy checking down and right one of the scripts will drop through to the down-only broadcast. But then you probably need another script to handle right or left on their own and it starts getting messy.

A better option is one script that checks the keys currently being pressed and only a single script that changes costumes and another that moves the sprite depending on what keys were pressed.

For example, you build up a text string so you start with a blank string and append 0 or 1 depending on the state of each key. So you check the up arrow and append 0 if not pressed, check down and append 1 if pressed etc so 0110 would be right and down. Then you know to move the player down if the second letter of your move code is 1.

For costumes my preference is a script that cycles between a min and max for the current range of costumes where the range depends on the current state. So, for example, 0110 might be recognised by a script as indicating a minimum costume number of 5 and max number of 8 so you have variables for min and max set to those values. The costume change script is then simply

forever
next costume
if <<(costume #) > (maxcostume)> or <(costume #) < (mincostume)>> then
switch costume to (mincostume)
wait [0.1] secs
end
As long as a consistent wait between costumes is OK. This means you don't have to wait to finish one costume cycle before starting another since it switches to a different cycle when the costume min or max changes.
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

deck26 wrote:

Leandro_2013 wrote:

I'm not that good at coding, could you make a remix of it so i can use it?
It's not hard. You want something like

forever
if <key [down v] pressed?> then // you'll want your letter key as well, I'm leaving that to you
if <key [left v] pressed?> then // you'll want your letter key as well, I'm leaving that to you
broadcast [left-and-down v]
else
broadcast [down-only v]
end
wait until <not <key [any v] pressed?>>
end
end
But if you also want to detect down and right you need to include the right key in the above - if you have another copy checking down and right one of the scripts will drop through to the down-only broadcast. But then you probably need another script to handle right or left on their own and it starts getting messy.

A better option is one script that checks the keys currently being pressed and only a single script that changes costumes and another that moves the sprite depending on what keys were pressed.

For example, you build up a text string so you start with a blank string and append 0 or 1 depending on the state of each key. So you check the up arrow and append 0 if not pressed, check down and append 1 if pressed etc so 0110 would be right and down. Then you know to move the player down if the second letter of your move code is 1.

For costumes my preference is a script that cycles between a min and max for the current range of costumes where the range depends on the current state. So, for example, 0110 might be recognised by a script as indicating a minimum costume number of 5 and max number of 8 so you have variables for min and max set to those values. The costume change script is then simply

forever
next costume
if <<(costume #) > (maxcostume)> or <(costume #) < (mincostume)>> then
switch costume to (mincostume)
wait [0.1] secs
end
As long as a consistent wait between costumes is OK. This means you don't have to wait to finish one costume cycle before starting another since it switches to a different cycle when the costume min or max changes.

Thank you so much!
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

deck26 wrote:

Leandro_2013 wrote:

I'm not that good at coding, could you make a remix of it so i can use it?
It's not hard. You want something like

forever
if <key [down v] pressed?> then // you'll want your letter key as well, I'm leaving that to you
if <key [left v] pressed?> then // you'll want your letter key as well, I'm leaving that to you
broadcast [left-and-down v]
else
broadcast [down-only v]
end
wait until <not <key [any v] pressed?>>
end
end
But if you also want to detect down and right you need to include the right key in the above - if you have another copy checking down and right one of the scripts will drop through to the down-only broadcast. But then you probably need another script to handle right or left on their own and it starts getting messy.

A better option is one script that checks the keys currently being pressed and only a single script that changes costumes and another that moves the sprite depending on what keys were pressed.

For example, you build up a text string so you start with a blank string and append 0 or 1 depending on the state of each key. So you check the up arrow and append 0 if not pressed, check down and append 1 if pressed etc so 0110 would be right and down. Then you know to move the player down if the second letter of your move code is 1.

For costumes my preference is a script that cycles between a min and max for the current range of costumes where the range depends on the current state. So, for example, 0110 might be recognised by a script as indicating a minimum costume number of 5 and max number of 8 so you have variables for min and max set to those values. The costume change script is then simply

forever
next costume
if <<(costume #) > (maxcostume)> or <(costume #) < (mincostume)>> then
switch costume to (mincostume)
wait [0.1] secs
end
As long as a consistent wait between costumes is OK. This means you don't have to wait to finish one costume cycle before starting another since it switches to a different cycle when the costume min or max changes.


Wait, I have a question, the code is good but I want it to make that you can change the direction your walking while your in another direction, I have to un-press the down button, press the left first and then the down button, can you give me an example of the code, please?
deck26
Scratcher
1000+ posts

My Mario moveset is glitchy! D:

Leandro_2013 wrote:

Wait, I have a question, the code is good but I want it to make that you can change the direction your walking while your in another direction, I have to un-press the down button, press the left first and then the down button, can you give me an example of the code, please?

If you use the method I suggested to get a code for the current keys which are down there shouldn't an issue. See https://scratch.mit.edu/projects/1012331671/
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

ok ill try it
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

IT WORKED TYSM
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

deck26 wrote:

Leandro_2013 wrote:

Wait, I have a question, the code is good but I want it to make that you can change the direction your walking while your in another direction, I have to un-press the down button, press the left first and then the down button, can you give me an example of the code, please?

If you use the method I suggested to get a code for the current keys which are down there shouldn't an issue. See https://scratch.mit.edu/projects/1012331671/

It lags ALOT! Even with TurboWarp, it crashes! Can you make a optimised version?
deck26
Scratcher
1000+ posts

My Mario moveset is glitchy! D:

You still have separate scripts for costume changes. My recommendation was a single script which only switches to the next costume in the current range.

Store the last key code - eg 0110 - in a variable. Let's call it lastcode.

Now at the end of your main input loop where you check the keys you can detect whether the newcode is different from the old one. If it is the same there is no need to do anything. If it has changed you set the correct values for the min and max costume numbers for that code and set lastcode to the new value. That's all you do for handling key input.

For costume changes you just have a forever loop switching to the next costume in the current set.

The main reason you're seeing lag is that you have a forever loop in a custom block set to run with no screen refresh. If you want to use that option you need to be sure the custom block can finish within one frame so you should never have a forever block unless you're interrupting it. In any case you call the custom block in a forever loop so why is there a forever loop in the custom block?
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

deck26 wrote:

You still have separate scripts for costume changes. My recommendation was a single script which only switches to the next costume in the current range.

Store the last key code - eg 0110 - in a variable. Let's call it lastcode.

Now at the end of your main input loop where you check the keys you can detect whether the newcode is different from the old one. If it is the same there is no need to do anything. If it has changed you set the correct values for the min and max costume numbers for that code and set lastcode to the new value. That's all you do for handling key input.

For costume changes you just have a forever loop switching to the next costume in the current set.

The main reason you're seeing lag is that you have a forever loop in a custom block set to run with no screen refresh. If you want to use that option you need to be sure the custom block can finish within one frame so you should never have a forever block unless you're interrupting it. In any case you call the custom block in a forever loop so why is there a forever loop in the custom block?
idk i thought that would work im not reallygood at scratch, but thankj you very much
WhyteTIGER
Scratcher
16 posts

My Mario moveset is glitchy! D:

Leandro_2013 wrote:

It lags ALOT! Even with TurboWarp, it crashes! Can you make a optimised version?

You have a custom block running without screen refresh and it has a loop inside which makes the project crash.
All you have to do is untick “run without screen refresh” in the custom block

Hey there!
I've been active on scratch for almost 5 years on different accounts. I enjoy creating, coding and sharing good quality projects and helping others with their code
My top 3 best projects:
1. HYPERCAR RACING
2. CUBLOX
3. FLAPPY BIRD - Mobile Friendly Remake
Leandro_2013
Scratcher
12 posts

My Mario moveset is glitchy! D:

WhyteTIGER wrote:

Leandro_2013 wrote:

It lags ALOT! Even with TurboWarp, it crashes! Can you make a optimised version?

You have a custom block running without screen refresh and it has a loop inside which makes the project crash.
All you have to do is untick “run without screen refresh” in the custom block

thanks

Powered by DjangoBB