Esempio n. 1
0
class ParaspritesExample(Sprite):

    def __init__(self):
        super(ParaspritesExample, self).__init__()

        background = Bitmap(BitmapData.load('./resources/ponyville.jpg'))
        background.width = WIDTH
        background.height = HEIGHT
        self.addChild(background)

        self.tile_layer = Sprite()
        self.addChild(self.tile_layer)

        parasprites_bd = BitmapData.load('./resources/parasprite_sheet.png')
        self.parasprites_ts = Tilesheet(parasprites_bd)

        twidth = parasprites_bd.width / 4.0
        theight = parasprites_bd.height / 3.0

        for i in range(3):
            for j in range(4): 
                self.parasprites_ts.addTileRect(
                    Rectangle(twidth * j, theight * i, twidth, theight),
                        Point(twidth * 0.5, theight * 0.5))

        self.parasprites = [ ]
        for i in range(START_PARASPRITES):
            self.parasprites.append(
                Parasprite(
                    random.uniform(WIDTH * 0.1, WIDTH * 0.9),
                        random.uniform(HEIGHT * 0.1, HEIGHT * 0.9),
                    ))

        self.addEventListener(Event.ENTER_FRAME, self.on_enter_frame)
        self.tile_layer.addEventListener(MouseEvent.CLICK, self.on_click)
        self.old_time = time()

    def on_enter_frame(self, event):
        new_time = time()
        dt = new_time - self.old_time

        tilesheet_data = []

        for parasprite in self.parasprites:
            parasprite.process(dt)
            tilesheet_data += parasprite.get_tile_data()

        self.tile_layer.graphics.clear()
        self.tile_layer.graphics.drawTiles(
                            self.parasprites_ts, tilesheet_data, 
                                Tilesheet.TILE_ROTATION | \
                                    Tilesheet.TILE_SCALE | \
                                        Tilesheet.TILE_SMOOTH)
        self.old_time = new_time

    def on_click(self, event):
        if len(self.parasprites) <= MAX_PARASPRITES:
            self.parasprites.append(Parasprite(event.stageX, event.stageY))
Esempio n. 2
0
    def __init__(self):
        super(MouseExample, self).__init__()

        spr = Sprite()
        spr.x = WIDTH * 0.25
        spr.y = HEIGHT * 0.25
        self.addChild(spr)

        gfx = spr.graphics
        gfx.beginFill(0xe6db74)
        gfx.drawRect(0.0, 0.0, WIDTH * 0.5, HEIGHT * 0.5)
        gfx.endFill()

        self.txt = TextField()
        self.txt.selectable = False
        self.txt.autoSize = TextFieldAutoSize.LEFT
        spr.addChild(self.txt)

        spr.addEventListener(MouseEvent.MOUSE_OVER, self.on_spr_mouse_over)
        spr.addEventListener(MouseEvent.MOUSE_OUT, self.on_spr_mouse_out)
        spr.addEventListener(MouseEvent.MOUSE_DOWN, self.on_spr_mouse_down)
        spr.addEventListener(MouseEvent.MOUSE_UP, self.on_spr_mouse_up)

        self.cursor = Sprite()
        self.cursor.mouseEnabled = False
        self.addChild(self.cursor)

        gfx = self.cursor.graphics
        gfx.lineStyle(2.0, 0x000000)
        gfx.beginFill(0xff0000)
        gfx.lineTo(0.0, 20.0)
        gfx.lineTo(15.0, 10.0)
        gfx.endFill()

        self.addEventListener(Event.ADDED_TO_STAGE, self.on_stage)