def main(): # save the file paths which need to be loaded loadList = [ {"name" : "player", "path" : "./images/player.png"}, {"name" : "bg", "path" : "./images/bg.jpg"}, {"name" : "item0", "path" : "./images/item0.png"}, {"name" : "item1", "path" : "./images/item1.png"}, {"name" : "item2", "path" : "./images/item2.png"}, {"name" : "item3", "path" : "./images/item3.png"}, {"name" : "item4", "path" : "./images/item4.png"}, {"name" : "item5", "path" : "./images/item5.png"}, {"name" : "item6", "path" : "./images/item6.png"}, {"name" : "item7", "path" : "./images/item7.png"} ] # create loading page loadingPage = LoadingSample1() addChild(loadingPage) def loadComplete(result): loadingPage.remove() gameInit(result) # load files LoadManage.load(loadList, loadingPage.setProgress, loadComplete)
def startGame(e): global beginningLayer, gameLayer beginningLayer.remove() gameLayer = GameLayer(dataList["bg"]) addChild(gameLayer)
def main(): controlLayer = Sprite() addChild(controlLayer) title = TextField() title.size = 25 title.text = "Choose A Loading Bar" controlLayer.addChild(title) btn1 = ButtonSample(text="LoadingSample1", size=30) btn1.y = 100 btn1.addEventListener(MouseEvent.MOUSE_UP, lambda e: addLoadingBar(1)) controlLayer.addChild(btn1) btn2 = ButtonSample(text="LoadingSample2", size=30) btn2.y = 180 btn2.addEventListener(MouseEvent.MOUSE_UP, lambda e: addLoadingBar(2)) controlLayer.addChild(btn2) btn3 = ButtonSample(text="LoadingSample3", size=30) btn3.y = 260 btn3.addEventListener(MouseEvent.MOUSE_UP, lambda e: addLoadingBar(3)) controlLayer.addChild(btn3) title.x = (controlLayer.width - title.width) / 2 controlLayer.x = (stage.width - controlLayer.width) / 2 controlLayer.y = (stage.height - controlLayer.height) / 2
def main(): # save the file paths which need to be loaded loadList = [ {"name" : "map", "path" : "./images/map.jpg"}, {"name" : "guanyu_mov", "path" : "./images/guanyu_mov.png"}, {"name" : "guanyu_atk", "path" : "./images/guanyu_atk.png"}, {"name" : "soldier1_atk", "path" : "./images/soldier1_atk.png"}, {"name" : "soldier1_mov", "path" : "./images/soldier1_mov.png"}, {"name" : "soldier2_mov", "path" : "./images/soldier2_mov.png"}, {"name" : "xiahoudun_atk", "path" : "./images/xiahoudun_atk.png"}, {"name" : "xiahoudun_mov", "path" : "./images/xiahoudun_mov.png"}, ] # create loading page loadingPage = LoadingSample2() addChild(loadingPage) def loadComplete(result): global dataList loadingPage.remove() dataList = result gameInit() # load files LoadManage.load(loadList, loadingPage.setProgress, loadComplete)
def main(): loadList = [] for root, dirs, files in os.walk("./res/images/"): for filename in files: if filename.split(".")[1] == "png": loadList.append({ "name": filename.split(".")[0], "path": root + filename }) for root, dirs, files in os.walk("./res/"): for filename in files: if filename.split(".")[1] == "wav": loadList.append({ "name": filename.split(".")[0], "path": root + filename }) loadingLayer = LoadingSample1() addChild(loadingLayer) def next(result): loadingLayer.remove() initCoverPage(result) LoadManage.load(loadList, loadingLayer.setProgress, next)
def gameInit(): global stageLayer, gameLayer, ctrlLayer, mapLayer, characterLayer, fps # create some layers stageLayer = Sprite() addChild(stageLayer) gameLayer = Sprite() stageLayer.addChild(gameLayer) mapLayer = Sprite() gameLayer.addChild(mapLayer) characterLayer = Sprite() characterLayer.mouseChildren = False gameLayer.addChild(characterLayer) ctrlLayer = Sprite() stageLayer.addChild(ctrlLayer) # add FPS fps = FPS() addChild(fps) addMap() initCtrlLayer() addEvents()
def initCoverPage(result): # receive the loaded data dataList = result # add the cover layer coverLayer = Sprite() addChild(coverLayer) # set the bg image coverBg = Bitmap(BitmapData(dataList["coverBg"], 0, 0, 520, 640)) coverLayer.addChild(coverBg) # define the state of buttons normal = Bitmap(BitmapData(dataList["normalStartBtn"])) over = Bitmap(BitmapData(dataList["actionStartBtn"])) down = Bitmap(BitmapData(dataList["actionStartBtn"])) startBtn = Button(normal, over, down, None) startBtn.x = 135 startBtn.y = 385 coverLayer.addChild(startBtn) def next(e): # remove the contents of cover layer coverLayer.remove() # init cover layer initSettingPage(dataList) startBtn.addEventListener(MouseEvent.MOUSE_UP, next)
def displayImg2(e): bmp2 = Bitmap(BitmapData(e.currentTarget.content)) bmp2.x = 400 bmp2.y = 10 bmp2.rotation = 15 bmp2.alpha = 0.8 addChild(bmp2)
def main(): layer = Sprite() layer.x = layer.y = 50 addChild(layer) layer.graphics.beginFill() layer.graphics.lineStyle(5, "green", 0.2) layer.graphics.drawRect(0, 0, 150, 150) layer.graphics.endFill() layer.graphics.beginFill() layer.graphics.lineStyle(5, "green", 0.8) layer.graphics.drawRect(200, 0, 150, 150) layer.graphics.endFill() layer.graphics.beginFill("lightgreen") layer.graphics.lineStyle(5, "green", 0.5) layer.graphics.drawRect(400, 0, 150, 150) layer.graphics.endFill() layer.graphics.beginFill() layer.graphics.lineStyle(5, "green", 0.2) layer.graphics.drawCircle(75, 275, 75) layer.graphics.endFill() layer.graphics.beginFill() layer.graphics.lineStyle(5, "green", 0.8) layer.graphics.drawCircle(275, 275, 75) layer.graphics.endFill() layer.graphics.beginFill("lightgreen") layer.graphics.lineStyle(5, "green", 0.5) layer.graphics.drawCircle(475, 275, 75) layer.graphics.endFill() layer.graphics.beginFill() layer.graphics.lineStyle(5, "green", 0.2) layer.graphics.moveTo(0, 498) layer.graphics.lineTo(75, 400) layer.graphics.lineTo(150, 498) layer.graphics.lineTo(0, 498) layer.graphics.endFill() layer.graphics.beginFill() layer.graphics.lineStyle(5, "green", 0.8) layer.graphics.moveTo(200, 498) layer.graphics.lineTo(275, 400) layer.graphics.lineTo(350, 498) layer.graphics.lineTo(200, 498) layer.graphics.endFill() layer.graphics.beginFill("lightgreen") layer.graphics.lineStyle(5, "green", 0.5) layer.graphics.moveTo(400, 498) layer.graphics.lineTo(475, 400) layer.graphics.lineTo(550, 498) layer.graphics.lineTo(400, 498) layer.graphics.endFill()
def main(): global loadingPage # save the file path which needs to be loaded loadList = [{"name": "bg", "path": "./images/bg.jpg"}] # create loading page loadingPage = LoadingSample2() addChild(loadingPage) # load file LoadManage.load(loadList, loadingPage.setProgress, gameInit)
def demoInit(result): m1 = Shape() m1.graphics.beginFill() m1.graphics.drawRect(50, 50, 100, 100) m1.graphics.drawCircle(25, 25, 25) m1.graphics.moveTo(200, 25) m1.graphics.lineTo(150, 125) m1.graphics.lineTo(250, 150) m1.graphics.closePath() m1.graphics.drawRect(40, 175, 150, 150) m1.graphics.drawCircle(250, 225, 50) img1 = Bitmap(BitmapData(result["avatar"])) img1.mask = m1 addChild(img1) img1_bg = Bitmap(BitmapData(result["avatar"])) img1_bg.alpha = 0.3 addChild(img1_bg) m2 = Shape() m2.graphics.beginFill() m2.graphics.drawRect(100, 100, 200, 200) m2.graphics.drawCircle(200, 200, 50) img2 = Bitmap(BitmapData(result["girl"])) img2.x = 320 img2.mask = m2 addChild(img2) img2_bg = Bitmap(BitmapData(result["girl"])) img2_bg.x = 320 img2_bg.alpha = 0.3 addChild(img2_bg)
def gameInit(result): global dataList, stageLayer dataList = result # create stage layer stageLayer = Sprite() addChild(stageLayer) # add FPS fps = FPS() addChild(fps) # add background bg = Bitmap(BitmapData(dataList["bg"])) stageLayer.addChild(bg) # add some text fields titleTxt = TextField() titleTxt.text = "Get Furit" titleTxt.size = 70 titleTxt.textColor = "red" titleTxt.x = (stage.width - titleTxt.width) / 2 titleTxt.y = 100 stageLayer.addChild(titleTxt) hintTxt = TextField() hintTxt.text = "Tap to Start the Game!~" hintTxt.textColor = "red" hintTxt.size = 40 hintTxt.x = (stage.width - hintTxt.width) / 2 hintTxt.y = 300 stageLayer.addChild(hintTxt) engineTxt = TextField() engineTxt.text = "- Powered by Pylash -" engineTxt.textColor = "red" engineTxt.size = 20 engineTxt.weight = TextFormatWeight.BOLD engineTxt.italic = True engineTxt.x = (stage.width - engineTxt.width) / 2 engineTxt.y = 500 stageLayer.addChild(engineTxt) # add event that the game will start when you click stageLayer.addEventListener(MouseEvent.MOUSE_UP, startGame) # add keyboard events to control player stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown) stage.addEventListener(KeyboardEvent.KEY_UP, keyUp)
def main(): global loadingBar loadList = [ {"name" : "bgm", "path" : "./sample.wav"}, {"name" : "1", "path" : "./1.jpg"}, {"name" : "2", "path" : "./2.jpg"}, {"name" : "3", "path" : "./3.jpg"}, {"name" : "4", "path" : "./4.jpg"} ] loadingBar = LoadingSample3() addChild(loadingBar) LoadManage.load(loadList, loadingBar.setProgress, demoInit)
def main(): global loadingBar loadList = [{ "name": "atk", "path": "./atk.png" }, { "name": "mov", "path": "./mov.png" }] loadingBar = LoadingSample2() addChild(loadingBar) LoadManage.load(loadList, loadingBar.setProgress, demoInit)
def demoInit(result): global loadingBar loadingBar.remove() imageList = [BitmapData(result["1"]), BitmapData(result["2"]), BitmapData(result["3"]), BitmapData(result["4"])] for i, bmpd in enumerate(imageList): bmp = Bitmap(bmpd) bmp.scaleX = bmp.scaleY = 0.3 bmp.x += i * 140 bmp.y += i * 150 addChild(bmp) sound = Sound(result["bgm"]) sound.loopCount = Sound.LOOP_FOREVER sound.play()
def addLoadingBar(index): global loopFrameIndex, progress loopFrameIndex = 0 progress = 0 loadingBar = None if index == 1: loadingBar = LoadingSample1() elif index == 2: loadingBar = LoadingSample2() elif index == 3: loadingBar = LoadingSample3() if not loadingBar: return loadingBar.addEventListener(LoopEvent.ENTER_FRAME, onFrame) addChild(loadingBar)
def gameInit(result): global beginningLayer, dataList, loadingPage loadingPage.remove() # save the data which is loaded dataList = result # create a Sprite object beginningLayer = Sprite() # push the Sprite object into display list to show addChild(beginningLayer) # add background bg = Bitmap(BitmapData(dataList["bg"], 330, 300)) beginningLayer.addChild(bg) # add some text fields title = TextField() title.text = "Find Character" title.font = "Microsoft YaHei" title.size = 60 title.weight = TextFormatWeight.BOLD title.x = (stage.width - title.width) / 2 title.y = 150 beginningLayer.addChild(title) hintTxt = TextField() hintTxt.text = "Tap to Start The Game" hintTxt.font = "Microsoft YaHei" hintTxt.size = 30 hintTxt.x = (stage.width - hintTxt.width) / 2 hintTxt.y = 300 beginningLayer.addChild(hintTxt) engineTxt = TextField() engineTxt.text = "- Powered by Pylash -" engineTxt.font = "Microsoft YaHei" engineTxt.size = 20 engineTxt.italic = True engineTxt.x = (stage.width - engineTxt.width) / 2 engineTxt.y = 400 beginningLayer.addChild(engineTxt) # add event that game will start when you click beginningLayer.addEventListener(MouseEvent.MOUSE_UP, startGame)
def main(): global loadingBar loadList = [{ "name": "1", "path": "./1.png" }, { "name": "2", "path": "./2.png" }, { "name": "3", "path": "./3.png" }] loadingBar = LoadingSample2() addChild(loadingBar) LoadManage.load(loadList, loadingBar.setProgress, demoInit)
def main(): t1 = TextField() t1.x = t1.y = 50 t1.size = 30 t1.text = "Hello World!" addChild(t1) t2 = TextField() t2.x = 50 t2.y = 150 t2.text = "Hello Pylash~" t2.rotation = 30 t2.size = 50 t2.textColor = "#FF4500" t2.weight = TextFormatWeight.BOLD addChild(t2) t3 = TextField() t3.x = 300 t3.y = 130 t3.text = "Based on Python3, PySide2" t3.alpha = 0.7 t3.size = 40 t3.textColor = "#0000FF" t3.italic = True addChild(t3)
def main(): global stageLayer stageLayer = Sprite() stageLayer.graphics.beginFill() stageLayer.graphics.drawRect(0, 0, stage.width, stage.height) stageLayer.graphics.endFill() addChild(stageLayer) # add events stageLayer.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove) stageLayer.addEventListener(MouseEvent.MOUSE_UP, mouseUp) # create a circle circle = DraggableShape(Circle(0, 0, 60)) circle.x = circle.y = 150 stageLayer.addChild(circle) # create a square square = DraggableShape(Polygon(getPolygonVertices(4, 70))) square.x = square.y = 350 stageLayer.addChild(square) # create a pentagon pentagon = DraggableShape(Polygon(getPolygonVertices(5, 80))) pentagon.x = 150 pentagon.y = 350 stageLayer.addChild(pentagon) # create a hexagon hexagon = DraggableShape(Polygon(getPolygonVertices(6, 90))) hexagon.x = 350 hexagon.y = 150 stageLayer.addChild(hexagon) # create a dodecagon dodecagon = DraggableShape(Polygon(getPolygonVertices(12, 100))) dodecagon.x = 580 dodecagon.y = 250 stageLayer.addChild(dodecagon)
def main(): btn1 = ButtonSample(text="Button 1") btn1.x = 100 btn1.y = 50 btn1.addEventListener(MouseEvent.MOUSE_DOWN, lambda e: print("Click Button 1")) addChild(btn1) btn2 = ButtonSample(text="Button 2", roundBorder=False, backgroundColor={ "normalState": "#00AA00", "overState": "#00FF00" }, lineColor="green", size=30, textColor="#222222") btn2.x = 100 btn2.y = 90 btn2.addEventListener(MouseEvent.MOUSE_DOWN, lambda e: print("Click Button 2")) addChild(btn2) lineEdit1 = LineEdit() lineEdit1.x = 100 lineEdit1.y = 150 addChild(lineEdit1) bgLayer = Sprite() bgLayer.graphics.beginFill("lightgreen") bgLayer.graphics.lineStyle(5, "green", 0.5) bgLayer.graphics.drawRect(0, 0, 300, 50) bgLayer.graphics.endFill() lineEdit2 = LineEdit(bgLayer) lineEdit2.text = "Input 2" lineEdit2.x = 100 lineEdit2.y = 200 lineEdit2.size = 30 lineEdit2.setMargins(10, 0, 10, 0) lineEdit2.addEventListener(LineEditEvent.FOCUS_IN, lambda e: print("Focus in Line Edit 2")) lineEdit2.addEventListener(LineEditEvent.FOCUS_OUT, lambda e: print("Focus out Line Edit 2")) addChild(lineEdit2)
def demoInit(result): global loadingBar loadingBar.remove() dataList = result bmp1 = Bitmap(BitmapData(dataList["1"])) bmp1.x = 530 bmp1.y = 30 addChild(bmp1) bmp2 = Bitmap(BitmapData(dataList["2"])) bmp2.x = 530 bmp2.y = 230 addChild(bmp2) bmp3 = Bitmap(BitmapData(dataList["3"])) bmp3.x = 530 bmp3.y = 460 addChild(bmp3) TweenLite.to(bmp1, 3, {"x": 30, "alpha": 0.5, "ease": Easing.Sine.easeOut}) TweenLite.to(bmp2, 3, { "x": 50, "rotation": 360, "ease": Easing.Back.easeIn }) TweenLite.to(bmp3, 3, { "x": 250, "y": 700, "scaleX": -1, "scaleY": -1, "ease": Easing.Quad.easeIn }).to(bmp3, 2, { "delay": 1, "scaleY": 1, "y": 460 })
def gameStart(data, bgmPlay, effectsPlay, p1Profile, p2Profile): global dataList, playingLayer, effectsOn, gameOverEffect, eatCandyEffects, moveEffect, btnEffect effectsOn = effectsPlay # receive the loaded data dataList = data gameOverEffect = Sound(dataList["gameOverEffect"]) eatCandyEffects = [Sound(dataList["eatCandyEffect1"]),Sound(dataList["eatCandyEffect2"]),Sound(dataList["eatCandyEffect3"])] for eatCandyEffect in eatCandyEffects: eatCandyEffect.loopCount = 1 moveEffect = Sound(dataList["moveEffect"]) moveEffect.loopCount = Sound.LOOP_FOREVER btnEffect = Sound(dataList["btnEffect"]) btnEffect.loopCount = 1 # create the playing layer playingLayer = Sprite() addChild(playingLayer) # set the bg image playingBg = Bitmap(BitmapData(dataList["playingBg"], 0, 0, 520, 640)) playingLayer.addChild(playingBg) # add profiles profile1 = Bitmap(BitmapData(dataList["profile%s" % p1Profile])) profile1.x = 20 profile1.y = 20 profile1.width = 100 profile1.height = 100 playingLayer.addChild(profile1) profile2 = Bitmap(BitmapData(dataList["profile%s" % p2Profile])) profile2.x = 400 profile2.y = 20 profile2.width = 100 profile2.height = 100 playingLayer.addChild(profile2) # display the scores colon = TextField() colon.text = ":" colon.size = 50 colon.x = 260 - colon.width/2 colon.y = 70 - colon.height/2 colon.textColor = "#225590" colon.font = "Bradley Hand" playingLayer.addChild(colon) global snake1, snake2 # create game snakes snake1 = snake("snake1", 60, 180, "Right") snake2 = snake("snake2", 450, 570, "Left") global score1Txt, score2Txt score1Txt = TextField() score1Txt.text = "%s" % snake1.score score1Txt.size = 50 score1Txt.x = 260 - 20 - score1Txt.width score1Txt.y = 70 - score1Txt.height/2 score1Txt.textColor = "#225590" score1Txt.font = "Bradley Hand" playingLayer.addChild(score1Txt) score2Txt = TextField() score2Txt.text = "%s" % snake2.score score2Txt.size = 50 score2Txt.x = 260 + 20 score2Txt.y = 70 - score2Txt.height/2 score2Txt.textColor = "#225590" score2Txt.font = "Bradley Hand" playingLayer.addChild(score2Txt) # add sample snakes sampleSnake1 = snake("snake1", 130, 40, "Up") sampleSnake2 = snake("snake2", 380, 40, "Up") stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown) stage.addEventListener(KeyboardEvent.KEY_UP, keyUp) playingLayer.addEventListener(LoopEvent.ENTER_FRAME, loop) generate(0.05)
def gameOver(): global stageLayer, selectedCharacter, start, enemyNum, isMouseDownAtGameLayer for o in AttackCharacter.ourList: o.animaSet.currentAnimation.removeAllEventListeners() o.animaSet.currentAnimation.stop() for o in Enemy.enemyList: o.animaSet.currentAnimation.removeAllEventListeners() o.animaSet.currentAnimation.stop() start = False stageLayer.mouseEnabled = False stageLayer.mouseChildren = False if selectedCharacter: selectedCharacter.remove() selectedCharacter = None enemyNum = 0 isMouseDownAtGameLayer = False resultLayer = Sprite() addChild(resultLayer) # add hint of game over hintTxt = TextField() hintTxt.textColor = "red" hintTxt.text = "Game Over!" hintTxt.weight = TextFormatWeight.BOLD hintTxt.size = 50 hintTxt.x = (stage.width - hintTxt.width) / 2 hintTxt.y = (stage.height - hintTxt.height) / 2 - 40 resultLayer.addChild(hintTxt) # add restart button def clickRestartBtn(e): global money, lives, loopIndex, stageLayer, fps, terrain AttackCharacter.ourList = [] Enemy.enemyList = [] loopIndex = 0 money = 200 lives = 10 terrain = copy.deepcopy(terrainList) stageLayer.remove() fps.remove() resultLayer.remove() gameInit() normal = Sprite() normal.graphics.beginFill("#666666") normal.graphics.lineStyle(3, "#222222") normal.graphics.drawRect(0, 0, 130, 60) normal.graphics.endFill() txt = TextField() txt.text = "RESTART" txt.textColor = "white" txt.size = 20 txt.x = (normal.width - txt.width) / 2 txt.y = (normal.height - txt.height) / 2 normal.addChild(txt) over = Sprite() over.graphics.beginFill("#888888") over.graphics.lineStyle(3, "#222222") over.graphics.drawRect(0, 0, 130, 60) over.graphics.endFill() txt = TextField() txt.text = "RESTART" txt.textColor = "white" txt.size = 20 txt.x = (over.width - txt.width) / 2 txt.y = (over.height - txt.height) / 2 over.addChild(txt) restartBtn = Button(normal, over) restartBtn.x = (stage.width - restartBtn.width) / 2 restartBtn.y = stage.height / 2 + 30 resultLayer.addChild(restartBtn) restartBtn.addEventListener(MouseEvent.MOUSE_UP, clickRestartBtn)
def displayImg1(e): bmp1 = Bitmap(BitmapData(e.target)) bmp1.x = bmp1.y = 10 addChild(bmp1)
def demoInit(result): global loadingBar, animaSet loadingBar.remove() # move image mov = result["mov"] # atk image atk = result["atk"] ### # create animation set to contain a sort of animations ### animaSet = AnimationSet() animaSet.x = animaSet.y = 100 addChild(animaSet) ### # move animation ### # get move animation frame list frameList = Animation.divideUniformSizeFrames(48, 96, 1, 2) # move down bmpd = BitmapData(mov, 0, 0, 48, 96) anima = Animation(bmpd, frameList) animaSet.addAnimation("move_down", anima) # move up bmpd = BitmapData(mov, 0, 96, 48, 96) anima = Animation(bmpd, frameList) animaSet.addAnimation("move_up", anima) # move left bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) animaSet.addAnimation("move_left", anima) # move right bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) anima.mirroring = True animaSet.addAnimation("move_right", anima) ### # attack animation ### # get atk animation frame list frameList = Animation.divideUniformSizeFrames(64, 256, 1, 4) # atk down bmpd = BitmapData(atk, 0, 0, 64, 256) anima = Animation(bmpd, frameList) animaSet.addAnimation("atk_down", anima) # atk up bmpd = BitmapData(atk, 0, 256, 64, 256) anima = Animation(bmpd, frameList) animaSet.addAnimation("atk_up", anima) # atk left bmpd = BitmapData(atk, 0, 512, 64, 256) anima = Animation(bmpd, frameList) animaSet.addAnimation("atk_left", anima) # atk right bmpd = BitmapData(atk, 0, 512, 64, 256) anima = Animation(bmpd, frameList) anima.mirroring = True animaSet.addAnimation("atk_right", anima) for n in animaSet.animationList: o = animaSet.animationList[n] o.playMode = AnimationPlayMode.VERTICAL o.speed = 5 # if atk animation... (because atk animation is bigger than move animation) if n.find("atk") >= 0: o.x -= 8 o.y -= 8 animaSet.changeAnimation("move_down") ### # create buttons ### btnList = [ "move down", "move up", "move right", "move left", "atk down", "atk up", "atk right", "atk left" ] btnLayer = Sprite() btnLayer.x = 200 btnLayer.y = 50 addChild(btnLayer) for i, n in enumerate(btnList): btn = ButtonSample(text=n) btn.label = n btnLayer.addChild(btn) btn.addEventListener(MouseEvent.MOUSE_DOWN, onBtnClick) btn.x = (0 if (4 - i) > 0 else 1) * 150 btn.y = (i % 4) * 50