Maybe someone knows a better way but I'm trying to test a way to move a sprite around the screen using a virtual d-pad at bottom left of the screen. I tried creating a global variable that is set to 0 at the beginning. When you touch the screen it changes to 1 and when you stop touching the screen it changes back to 0 again.
In update() I expected touch() to change the global variable on the fly but it just does not work like I thought. Any help would be appreciated.
```
from scene import *
import sound
Global variable that will be changed when you touch the screen.
sp = 0
class MyScene (Scene):
def setup(self):
self.background_color = 'midnightblue'
self.ship = SpriteNode('spc:PlayerShip1Orange')
self.ship.position = self.size / 2
self.add_child(self.ship)
def touch_began(self, touch):
if touch.location < 100:
# when the screen is touched then the global variable changes to 1 and should update the movement of the ship in update()... but ship movement does not change.
sp = 1
def touch_ended(self, touch):
if touch.location < 100:
# i was hoping that once touch_began() started moving ship, that touch_ended() would stop movement so that it works like a simple directional pad.
sp = 0
def update(self):
pos = self.ship.position
'''pos += x + sp, y + sp'''
#this is just a test to see if touch would change the global variable "sp" and update accordingly. If i change it on line 4 it will update and move the ship. I dont know how to alter sp on the fly to move ship with touch until i stop touching the screen. like a controler moving mario or something.
pos.y += sp
# Don't allow the ship to move beyond the screen bounds:
pos.x = max(0, min(self.size.w, pos.x))
pos.y = max(0, min(self.size.h, pos.y))
self.ship.position = pos
run(MyScene(), PORTRAIT)```