button - How should I set up my touch controls in starling? -
i developing game in starling has 2 controls. 1 users left thumb touching anywhere on left side of screen, drags character , down y axis. other control having trouble implementing single button in bottom right of screen make character fire bullet.
my question how set without having character jump down bottom of screen whenever button pressed.
will need mess multi-touch in order running?
a more specific question have how define rectangle sprite in starling? since starling has no drawing api, can't this...
touchlayer.graphics.beginfill(0x000000, 0); touchlayer.graphics.drawrect(0, 0, stage.stagewidth, stage.stageheight); touchlayer.graphics.endfill();
currently character drag working, touch registers everywhere on screen (im not sure how make left side of screen...)
any advise appreciated, thank you.
here complete code ingame class requested.
package screens { import flash.geom.point; import flash.geom.rectangle; import flash.utils.gettimer; import objects.enemy; import objects.gamebackground; import objects.laser; import objects.scarlet; import objects.snowball; import starling.display.button; import starling.display.sprite; import starling.events.event; import starling.events.touch; import starling.events.touchevent; import starling.utils.rectangleutil; import starling.utils.deg2rad; public class ingame extends sprite { private var startbtn:button; private var firebtn:button; private var bg:gamebackground; private var scarlet:scarlet; private var enemies:array; private var lasers:array; private var scarletlocation:point; private var laserslayer:sprite; private var enemieslayer:sprite; private var touchlayer:sprite; private var enemyspawndelay:number; private var enemyspawncounter:number; private var difficulty:number; private var difficultyrate:number; private var timeprevious:number; private var timecurrent:number; private var elapsed:number; private var gamestate:string; private var playerspeed:number; private const min_speed:number = 650; //private var scoredistance:int; private var gamearea:rectangle; private var toucharea:rectangle; private var shape:starling.display.sprite = new starling.display.sprite(); private var target:point = new point(100, 100); private var touch:touch; private var touchx:number; private var touchy:number; public function ingame() { super(); this.addeventlistener(starling.events.event.added_to_stage, onaddedtostage); } private function onaddedtostage(event:event):void { this.removeeventlistener(event.added_to_stage, onaddedtostage); drawgame(); } private function drawgame():void { bg = new gamebackground(); this.addchild(bg); scarlet = new scarlet; scarlet.x = stage.stagewidth/2; scarlet.y = stage.stageheight/2; this.addchild(scarlet); startbtn = new button(assets.gettexture("playbtn")); startbtn.x = stage.stagewidth * 0.5 - startbtn.width * 0.5; startbtn.y = stage.stageheight * 0.5 - startbtn.height * 0.5 + 35; this.addchild(startbtn); firebtn = new button(assets.gettexture("firebtn")); firebtn.x = 675; firebtn.y = 435; this.addchild(firebtn); //defines area scarlet can fly in gamearea = new rectangle(0, 15, stage.stagewidth, stage.stageheight - 150); } public function disposetemporarily():void { this.visible = false; } public function initialize():void { this.visible = true; this.addeventlistener(event.enter_frame, checkelapsed); scarlet.x = -stage.stagewidth; scarlet.y = stage.stageheight * 0.5; gamestate = "idle"; playerspeed = 0; difficultyrate = 0.3; difficulty = 1; enemyspawndelay = enemyspawncounter = 100; enemies = new array(); lasers = new array(); scarletlocation = new point(200, 400); addeventlistener(event.enter_frame, update); laserslayer = new sprite(); enemieslayer = new sprite(); touchlayer = new sprite(); addchild(laserslayer); addchild(enemieslayer); addchild(touchlayer); addeventlistener(event.added_to_stage, setuptouchlayer); touchlayer.addeventlistener(event.triggered, shootlaser); //scoredistance = 0; startbtn.addeventlistener(event.triggered, onstartbtnclick); this.addeventlistener(touchevent.touch, ontouch); } private function onstartbtnclick(event:event):void { startbtn.visible = false; startbtn.removeeventlistener(event.triggered, onstartbtnclick); launchscarlet(); } private function launchscarlet():void { this.addeventlistener(touchevent.touch, ontouch); this.addeventlistener(event.enter_frame, ongametick); } private function ontouch(event:touchevent):void { touch = event.gettouch(stage); touchx = touch.globalx; touchy = touch.globaly; target.x = event.touches[0].globalx; target.y = event.touches[0].globaly; } private function ongametick(event:event):void { switch(gamestate) { case"idle": //take off if (scarlet.x < stage.stagewidth * 0.5 * 0.5) { scarlet.x += ((stage.stagewidth * 0.5 * 0.5 + 10) - scarlet.x) * 0.5; scarlet.y = stage.stageheight * 0.5; playerspeed += (min_speed - playerspeed) * 0.05; } else { gamestate = "flying"; } break; case"flying": playerspeed -= (playerspeed - min_speed) * 0.01; scarlet.y -= (scarlet.y - touchy) * 0.1; if (-(scarlet.y - touchy) < 150 && -(scarlet.y - touchy) > -150) { scarlet.rotation = deg2rad(-(scarlet.y - touchy) * 0.075); } if (scarlet.y > gamearea.bottom - scarlet.height * 0.5) { scarlet.y = gamearea.bottom - scarlet.height * 0.5; scarlet.rotation = deg2rad(0); } if (scarlet.y < gamearea.top + scarlet.height * 0.5) { scarlet.y = gamearea.top + scarlet.height * 0.5; scarlet.rotation = deg2rad(0); } //scoredistance += (playerspeed * elapsed) * 0.1; //trace(scoredistance); break; case"over": break; } } //addapted "shoot" private function setuptouchlayer(event:event):void { touchlayer.graphics.beginfill(0x000000, 0); touchlayer.graphics.drawrect(0, 0, stage.stagewidth, stage.stageheight); touchlayer.graphics.endfill(); toucharea = new rectangle(0, 0, stage.stagewidth, stage.stageheight); } private function shootlaser(event:touchevent):void { makelaser(scarletlocation); } private function makelaser(scarletlocation:point):void { var newlaser:laser = new laser(); newlaser.x = scarletlocation.x; newlaser.y = scarletlocation.y; newlaser.xvel = 5; newlaser.yvel = 5; newlaser.addeventlistener(laser.purge_event, purgelaserhandler); laserslayer.addchild(newlaser); lasers.push(newlaser); } private function purgelaserhandler(event:event):void { var targetlaser:laser = laser(event.target); purgelaser(targetlaser); } private function purgelaser(targetlaser:laser):void { targetlaser.removeeventlistener(laser.purge_event, purgelaserhandler); try { var i:int; (i = 0; < lasers.length; i++) { if (lasers[i].name == targetlaser.name) { lasers.splice(i, 1); laserslayer.removechild(targetlaser); = lasers.length; } } } catch(e:error) { trace("failed delete laser!", e); } } private function makeenemies():void { enemyspawncounter++; if (enemyspawncounter > enemyspawndelay) { enemyspawncounter = 0; enemyspawndelay -= difficultyrate; difficulty += difficultyrate; makeenemy(); } } private function makeenemy():void { var i:int; (i = 0; < math.floor(difficulty); i++) { var newenemy:enemy = new snowball(); newenemy.x = 925; newenemy.y = math.random() * 375 + 50; //trace(newenemy.x); trace(newenemy.y); newenemy.xvel = (-math.random() * difficulty) - 5; newenemy.sinmeter = math.random() * 10; newenemy.bobvalue = math.random() * difficulty; newenemy.addeventlistener(enemy.purge_event, purgeenemyhandler); enemieslayer.addchild(newenemy); enemies.push(newenemy); } } private function purgeenemyhandler(event:event):void { var targetenemy:enemy = enemy(event.target); purgeenemy(targetenemy); } private function purgeenemy(targetenemy:enemy):void { targetenemy.removeeventlistener(enemy.purge_event, purgelaserhandler); try { var i:int; (i = 0; < enemies.length; i++) { if (enemies[i].name == targetenemy.name) { enemies.splice(i, 1); enemieslayer.removechild(targetenemy); = enemies.length; } } } catch(e:error) { trace("failed delete enemy!", e); } } private function newhittest(laser:laser):void { each (var enemy:enemy in enemies) { if (enemy.status != "dead" && enemy.hittest(new point(laser.x, laser.y))) { enemy.destroy(); purgelaser(laser); } } } private function checkelapsed(event:event):void { timeprevious = timecurrent; timecurrent = gettimer(); elapsed = (timecurrent - timeprevious) * 0.001; } private function update():void { //trace(enemies.length, lasers.length); each (var enemy:enemy in enemies) { enemy.update(); } each (var laser:laser in lasers) { laser.update(); newhittest(laser); } makeenemies(); } //addapted "shoot" } }
oh wow, simple way solve problem enable multi-touch.
starling.multitouchenabled = true;
however still leaves problem of character jumping bottom of screen when press button in bottom right if not touching screen control character... not big deal better if didn't happen.
Comments
Post a Comment