Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Using SceneView to change between scenes
-
I have been experimenting with creating a Scene and adding a number of layers inside the scene and capturing the different touch events, this works great.
Now I would like to have a way of switching between two scenes when a user taps on the screen (each scene would ideally have a different layer layout and different touch actions).
I am keen to use SceneView so that i can also use buttons, etc. A started with a simple example to call my Scene that works well:
sv = SceneView() sv.scene = MyScene() sv.present('sheet')
I then tried to create a new class where i would overload the touch_ended function to switch to the other scene:
class MyView(SceneView): def __init__(self): self.scene = MyScene() def touch_ended(self,touch): self.scene = OtherScene()
But the python interpreter complains that the type 'scene SceneView' is not an acceptable base type...
I'm new to iOS and to this ui module, so i'm probably going about it the wrong way, any pointers very welcome
Thanks
Eduardo -
Unfortunately, the only ui type that can be subclassed is
ui.View
.You can create a custom view which contains a sceneview, to emulate much of this action. Although the sceneview will steal your touch events, unless you cover it with a nearly transparent view which can capture touch events, and then could pass onto the sceneview.
Alternatively, in your MyScene() class, you could capture touch events, and take whatever action you want. For instance if you pass in the parent sceneview into MyScene, so you have access to it within the scene touch events.
A third option would be to just have buttons displayed on top of your sceneview (say, a menu button) which then allows you to take some other action.
-
Hi JonB
Thanks for the info, sounds promising. Would you mind giving me a quick example (no functionality needed), on how you would create a custom view that would contain a sceneview? -
SceneViewer is a custom view that contains a scene.SceneView.
import scene, ui class MyScene(scene.Scene): def draw(self): scene.background(0, 0, 0) scene.fill(0, 0, 1) for touch in self.touches.values(): scene.ellipse(touch.location.x - 50, touch.location.y - 50, 100, 100) class SceneViewer(ui.View): def __init__(self, in_scene): self.present() self.scene_view = scene.SceneView(frame=self.bounds) self.scene_view.scene = in_scene self.add_subview(self.scene_view) SceneViewer(MyScene())
-
Thanks ccc
I'll give that a try -
I've played around with it a bit and can't figure out how to switch between two Scenes, say for example as a result of a touch.
In other words if i've got two scenes and a scene viewer like in this simple example, what would be a possible/recommended way of switching from one to the other?
import scene, ui class MyScene1(scene.Scene): def draw(self): scene.background(0, 0, 0) scene.fill(0, 0, 1) for touch in self.touches.values(): scene.ellipse(touch.location.x - 50, touch.location.y - 50, 100, 100) class MyScene2(scene.Scene): def draw(self): scene.background(0, 0, 0) scene.fill(1, 0, 1) for touch in self.touches.values(): scene.rect(touch.location.x - 50, touch.location.y - 50, 100, 100) class SceneViewer(ui.View): def __init__(self, in_scene): self.present() self.scene_view = scene.SceneView(frame=self.bounds) self.scene_view.scene = in_scene self.add_subview(self.scene_view) def draw(self): ... Switching code to MyScene2 here? ..... def touch_ended(self,touch): ...Switching code to MyScene2 here?... SceneViewer(MyScene())
Thanks for your help !!
-
Get the first scene.Scene to create the second... http://omz-forums.appspot.com/pythonista/post/5876387541942272
-
Thanks, i'll give that a go