This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-01-04 16:05:32

phoenixstormcrow
New Scratcher
Registered: 2013-01-04
Posts: 5

Problem moving sprite with arrow keys

I'm using a standard method to move a sprite up and down with the arrow keys:

    when green flag clicked
        go to x: -220 y: 0
        set "speed" to "10"
        forever  
            if (key "up arrow" pressed?) 
                point in direction 0
                move (speed) steps
            if (key "down arrow" pressed?) 
                point in direction 180
                move (speed) steps
    end
My problem is that if I'm holding down either the up or the down arrow to keep my sprite moving, and if I press any other key while doing so (for example, the spacebar to fire a laser or something) what happens is that the sprite will continue to move in the direction it was going, even if I stop pressing the arrow.

Let's say I'm moving up by holding the up arrow, and I whack the spacebar to shoot. Then I decide to move down, so I let off the up arrow, and press the down arrow.  What should happen is that the sprite should move down. What actually happens is that it simply stops moving completely as long as I'm holding the down arrow, and then begins moving up again when I release the down arrow. This makes it very difficult to change direction!

A workaround I've found is to press the up arrow again and let go. Then the sprite will respond normally, until I again try to fire while moving.

What seems to be happening is that when I press space, the script bound to that event executes (the firing script), but then control does not return to the top of the forever loop I posted above.

Any advice on how I can resolve this problem?

Offline

 

#2 2013-01-04 16:45:15

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Problem moving sprite with arrow keys

Add a "Until <NOT (up arrow pressed)>"

Right now, you are telling it that if the up or down arrows are ever pressed, to FOREVER keep moving up/down.


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#3 2013-01-04 17:07:42

phoenixstormcrow
New Scratcher
Registered: 2013-01-04
Posts: 5

Re: Problem moving sprite with arrow keys

A good idea, but it doesn't change the behavior.  If I'm moving up (or down) and press the space bar, the sprite continues moving in whichever direction, even if I stop pressing anything. If it is stuck moving up, and I press down, it will pause, and then resume its upward motion when I release down. The only way I can find to stop the upward motion is to press up again.

Offline

 

#4 2013-01-04 17:09:08

Kileymeister_test
Scratcher
Registered: 2010-01-31
Posts: 100+

Re: Problem moving sprite with arrow keys

This sounds more like a problem with your keyboard than the scripting.  Some keyboards stick when three or more buttons are pressed at the same time, unfortunately.  However, if you include the scripting for the actions that happen when you press the spacebar, maybe we can find another problem and fix it.

Maybe you could post the full script here?

Last edited by Kileymeister_test (2013-01-04 17:09:43)


Kileymeister's test account.  I upload engines and demos and stuff for public use.
Currently Working on: Grapple Arms!

Offline

 

#5 2013-01-04 17:23:11

phoenixstormcrow
New Scratcher
Registered: 2013-01-04
Posts: 5

Re: Problem moving sprite with arrow keys

My keyboard is brand new (as is the laptop it is attached to), so I hope it's not sticking.

Here is the firing script, attached to a bullet sprite:

   when [space] key pressed
        if ((this) = (bullet)) 
            set "ghost" effect to 0
            go to s[ ]
            repeat until (touching s[ ]?)  
                move 10 steps
            set "ghost" effect to 100
            go to s[ ]
    end
"this" is a variable private to the bullet sprite; part of an implementation of a primitive queue (so that I can fire multiple bullets.  All of that is working correctly.

It seems that the problem is simply that when the space bar is pressed, control is transferred to this firing script, and the "shooter" sprite just keeps doing whatever it was doing when that happened. I need a way to interrupt the forever loop I originally posted, and reenter it from the beginning.

Offline

 

#6 2013-01-04 17:29:15

phoenixstormcrow
New Scratcher
Registered: 2013-01-04
Posts: 5

Re: Problem moving sprite with arrow keys

Sorry, this is the correct script, not the previous one:

    when [space] key pressed
        if ((this) = (bullet)) 
            set "ghost" effect to 0
            go to s[Sprite1]
            repeat until (touching s[edge]?)  
                move 10 steps
            end
            set "ghost" effect to 100
            go to s[Sprite1]
        end

Offline

 

#7 2013-01-04 17:48:53

technoguyx
Scratcher
Registered: 2008-10-18
Posts: 1000+

Re: Problem moving sprite with arrow keys

mythbusteranimator wrote:

Add a "Until <NOT (up arrow pressed)>"

Right now, you are telling it that if the up or down arrows are ever pressed, to FOREVER keep moving up/down.

No - what he's doing right now is checking, every frame, whether the arrow keys are pressed, and if so, move a bit in the corresponding direction, giving the illusion of constant movement.

...And that is correct, so I don't know what the problem here might be.  hmm


http://getgnulinux.org/links/en/linuxliberated_4_78x116.png

Offline

 

#8 2013-01-04 20:04:40

ianthestuntman
Scratcher
Registered: 2013-01-02
Posts: 100+

Re: Problem moving sprite with arrow keys

@mythbusteranimator, love you signature (C)

Offline

 

#9 2013-01-04 20:23:54

-Stendika-
Scratcher
Registered: 2009-11-12
Posts: 74

Re: Problem moving sprite with arrow keys

First of all, why is the second if inside the first one? Did you mean to do that, or did you just transcribe it wrong?

Try this (if it isn't what you're using already). It works for me.

    when green flag clicked
        go to x: (-220) y: (0)
        set [speed v] to (10)
        forever  
            if <key [up arrow v] pressed?>
                point in direction (0 v)
                move (speed) steps
            end
            if <key [down arrow v] pressed?>
                point in direction (180 v)
                move (speed) steps
            end
    end

Last edited by -Stendika- (2013-01-04 20:24:56)


http://i1336.photobucket.com/albums/o660/Stendika/gbanner1_zpsd0178027.png
My latest games can be found here. Please view and give feedback!

Offline

 

#10 2013-01-04 23:28:51

bob6
Scratcher
Registered: 2010-07-01
Posts: 100+

Re: Problem moving sprite with arrow keys

Oh, yes. This problem again. @OP: Do you happen to be on a Ubuntu computer? Because I have this glitch too. Don't worry, you can still post the game and the glitch will stop happening on the web. It's not a scripting problem, but a problem with Scratch itself. Now to mention it, I hope Scratch will fix this bug.


http://i46.tinypic.com/3148ksz.gif

Offline

 

#11 2013-01-05 11:59:27

phoenixstormcrow
New Scratcher
Registered: 2013-01-04
Posts: 5

Re: Problem moving sprite with arrow keys

I am indeed running Ubuntu 12.04, and I had pretty much decided to chalk this up as a glitch beyond my control (I've tried a few other methods of managing the events in question, all with the same results). It's good to have confirmation of my suspicion.

And @Stendika, yes, my transcription was a typo. I just pasted the relevant portion of the project summary without too much fiddling around.

Thanks, everyone, for your input.

Offline

 

Board footer