0

I'm working on an assignment in which I have to make a sprite move. I've drawn each frame and I want to switch costumes to make it look like it's moving.

My block looks like this:

define walking
    forever
       switch costume to walk1
       switch costume to walk2

And my code looks like this:

when *flag* clicked
    forever
        if key right arrow pressed? then
            change x by 10
            walking

But nothing is happening.

rotpar
  • 11

4 Answers4

3

In order to figure out how this works, let's take a look at your code actually works. Here's your code in Scratchblocks.

So if you look at that, you'll notice there are two forever loops. Here's how your code is actually running:

when flag clicked
forever
    if <key [right arrow v] pressed?> then
        change x by (10)
        forever
            switch costume to [walk1 v]
            switch costume to [walk2 v]
        end
    end
end

See it in Scratchblocks.

It gets stuck in the switch-costume loop once you've pressed the right arrow key! By removing the forever loop from your custom block definition it should be fixed.

Nebula
  • 6,614
  • 4
  • 20
  • 40
0

When you put two blocks of "switch costume to[]" they will change very fast and for you will looks like they're not changing, so to be visible you need to put one timer after each block of switch costume and removing the forever block, like this:

when flag clicked
forever
    if <key [right arrow v] pressed?> then
        switch costume to [walk1 v]
        wait 0.5 secs
        change x by (10)
        switch costume to [walk2 v]
        wait 0.5 secs
    end
end

to a better visual effect I put the change x by (10) in the middle of them See it in Scratchblocks

Dado
  • 1
  • 1
0

Simple fix: Remove the forever loop from the define block

define walking:                â—„Fixed script 
    switch costume to (1)
    switch costume to (2)
Acolyer
  • 3
  • 3
0

For even better graphics, you can do this:

When FLAG clicked
    forever
      if <key [right arrow v] pressed?> then
      next costume
      wait 0.1 secs
      change x by(10)
    end
 end 

See in scratch blocks