hi guys!
having a really disturbing issue here

I tried creating a really simple platformers game, inspired by the
example provided in the test/test_platformer.py from the Downloads
section.
I just started using python so maybe i'm missing something trivial
The example works like a charm it's just that when I try to implement
it into a class it gives me errors. Here's the code:
[code]

class PlatformerController(actions.Action, tiles.RectMapCollider):
    on_ground = True
    MOVE_SPEED = 200
    JUMP_SPEED = 500
    GRAVITY = -1500

    def start(self):
        # initial velocity
        self.target.velocity = (0, 0)

    def step(self, dt):
        #print globals()
        dx, dy = self.target.velocity

        # using the player controls, gravity and other acceleration
influences
        # update the velocity
        dx = (keyboard[key.RIGHT] - keyboard[key.LEFT]) *
self.MOVE_SPEED *dt
        dy = dy + self.GRAVITY * dt
        if self.on_ground and keyboard[key.SPACE]:
            dy = self.JUMP_SPEED

        # get the player's current bounding rectangle
        last = self.target.get_rect()
        new = last.copy()
        new.x += dx
        new.y += dy * dt

        # run the collider
        dx, dy = self.target.velocity = self.collide_map(map, last,
new, dy, dx)
        self.on_ground = bool(new.y == last.y)

        # player position is anchored in the center of the image rect
        self.target.position = new.center

        # move the scrolling view to center on the player
        scroller.set_focus(*new.center)

class MainMenu(Menu):
    def __init__( self ):
        ...
        self.on_new_game()

    def on_new_game( self ):
        global keyboard, map, scroller
        director.init(width=800, height=600, do_not_scale=True)

        # create a layer to put the player in
        player_layer = layer.ScrollableLayer()
        # NOTE: the anchor for this sprite is in the CENTER (the cocos
default)
        # which means all positioning must be done using the center of
its rect
        player = cocos.sprite.Sprite('static/images/players/
witch.png')
        player_layer.add(player)
        player.do(PlatformerController())

        # add the map and the player sprite layer to a scrolling
manager
        scroller = layer.ScrollingManager()
        map = tiles.load('static/maps/world1.xml')['level0']
        scroller.add(map, z=0)
        scroller.add(player_layer, z=1)

        # set the player start using the player_start token from the
map
        start = map.find_cells(player_start=True)[0]
        r = player.get_rect()

        # align the mid bottom of the player with the mid bottom of
the start cell
        r.midbottom = start.midbottom

        # player image anchor (position) is in the center of the
sprite
        player.position = r.center

        # construct the scene with a background layer color and the
scrolling layers
        platformer_scene = cocos.scene.Scene()
        platformer_scene.add(layer.ColorLayer(100, 120, 150, 255),
z=0)
        platformer_scene.add(scroller, z=1)

        # track keyboard presses
        keyboard = key.KeyStateHandler()
        director.window.push_handlers(keyboard)

        # run the scene
        director.run(platformer_scene)


director.init(width=800, height=600, do_not_scale=True)
director.set_depth_test()
menulayer = MultiplexLayer(MainMenu(), ...)
scene = Scene(menulayer)
director.run(scene)
[/code]

after adding the "global keyboard, map, scroller" line, there are no
more errors, but when i run the script my player appears and suddenly
disappears very fast. If i'd try to comment the
player.do(PlatformerController()) line, it doesn't disappear, so
obviously I'm missing something...

Thanks for the help

-- 
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cocos-discuss?hl=en.

Reply via email to