actionscript 3 - Define rectangle how to -
i 'm trying develop game.in project have movieclip character game.i have 5 buttons(left right down , stop) user can move.the game android.in library have square dimnesions 50 x 50.i have filled stage copies of square different instances character can move on them.the point of game user moves character , hittestobject() function removes square walking on.i used timer if 5 seconds on , player hasn't completed rectangle of missing squares squares appear again.but if makes rectangle(lets makes removing 8 squares) squares inside rectangle must dissapear also.i need find way see when player completes rectangle.i think aproach want draft , there better one.
thanks in advance!
i think may haven't explained problem well..sorry mistake!everytime character touches on square disapears.if in 5 seconds character hasn't completed "rectangle" of missing squares appear again.when rectangle mean rectangle of squares.for example if 4 squares missing(not in row) rectangle,or if 8 squares or 12 etc...
import flash.utils.timer; stop(); multitouch.inputmode = multitouchinputmode.touch_point; var hunterxspeed:number=0; var hunteryspeed:number=0; var timerformove:timer=new timer(30); var timerforblocks:number=0; var simpletimer:number=0; var rectanglearray:array = new array(); var sumvertical:int=0; var sumhorizontal:int=0; var i:int=0; moveup.addeventlistener(touchevent.touch_tap, fl_taphandler_2); function fl_taphandler_2(event:touchevent):void { hunteryspeed = -4; hunterxspeed = 0; rectanglearray[0] = 0; rectanglearray[1] = -1; sumvertical+=rectanglearray[0]+rectanglearray[1]; i++; } movedown.addeventlistener(touchevent.touch_tap, fl_taphandler_3); function fl_taphandler_3(event:touchevent):void { hunteryspeed = 4; hunterxspeed = 0; rectanglearray[0] = 0; rectanglearray[1] = 1; sumvertical+=rectanglearray[0]+rectanglearray[1]; i++; } moveright.addeventlistener(touchevent.touch_tap, fl_taphandler_4); function fl_taphandler_4(event:touchevent):void { hunterxspeed = 4; hunteryspeed = 0; hunter.scalex = 2/4; rectanglearray[2] = 1; rectanglearray[3] = 0; sumhorizontal+= rectanglearray[2] +rectanglearray[3]; i++; } moveleft.addeventlistener(touchevent.touch_tap, fl_taphandler_5); function fl_taphandler_5(event:touchevent):void { hunterxspeed = -4; hunteryspeed = 0; hunter.scalex = -(2/4); rectanglearray[2] = -1; rectanglearray[3] = 0; sumhorizontal+= rectanglearray[2] + rectanglearray[3]; i++; } stopmove.addeventlistener(touchevent.touch_tap, fl_taphandler_6); function fl_taphandler_6(event:touchevent):void { hunterxspeed = 0; hunteryspeed = 0; } timerformove.addeventlistener(timerevent.timer,huntermove); function huntermove(e:timerevent) :void { if (hunter.y > stage.stageheight-160) { hunter.y = stage.stageheight-161; hunteryspeed = 0; } if (hunter.y < 20) { hunter.y = 21; hunteryspeed = 0; } if (hunter.x > stage.stagewidth-30) { hunter.x = stage.stagewidth-31; hunterxspeed = 0; } if (hunter.x < 30) { hunter.x = 31; hunterxspeed = 0; } hunterlegs.y = hunter.y+20; hunterlegs.x = hunter.x; hunter.y += hunteryspeed; hunter.x += hunterxspeed; if(timerforblocks==5000) { simpletimer+=50; } if(simpletimer>timerforblocks) { simpletimer=0; timerforblocks=0; } if(hunterlegs.hittestobject(grass)) { timerforblocks=5000; grass.x=grass.x-100; } if(hunterlegs.hittestobject(grass1)) { timerforblocks=5000; grass1.x=grass1.x-100; } if(hunterlegs.hittestobject(grass2)) { timerforblocks=5000; grass2.x=grass2.x-200; } if(hunterlegs.hittestobject(grass12)) { timerforblocks=5000; grass12.x=grass12.x-300; } if(hunterlegs.hittestobject(grass22)) { timerforblocks=5000; grass22.x=grass22.x-300; } if(hunterlegs.hittestobject(grass21)) { timerforblocks=5000; grass21.x=grass21.x-300; } if(hunterlegs.hittestobject(grass20)) { timerforblocks=5000; grass20.x=grass20.x-300; } if(hunterlegs.hittestobject(grass10)) { timerforblocks=5000; grass10.x=grass10.x-300; } if(sumvertical==0 && sumhorizontal==0 && i==4) { grass.x=2000; grass1.x=2000; grass2.x=2000; grass12.x=2000; grass22.x=2000; grass21.x=2000; grass20.x=2000; grass10.x=2000; } } timerformove.start();
all objects in first frame. named grass first square , copied 100 more or less different instances. hunter character.
a rectangle has 4 corners, , character ends @ origin square.
what try keep array store movement of character. instance, if character moves upward, store {x: 0, y: -1}
, if moves left, store {x: 1, y: 0}
.
then, when it's time check valid rectangles, step through list of movements , keep track of corners (where direction changes) , check see if following criteria fulfilled:
- the sum of horizontal movements 0
- the sum of vertical movements 0
- the sum of corners 4
this check see if character ended in same position started in , if it's movements formed valid rectangle.
if criteria isn't met, same check of array, start on position number 2. etc.
this way, check if valid rectangle has been formed at stage during character's movements (even if rectangle part of character's total moves). validate rectangle of size.
here's illustration perhaps clear things up:
in first case, when horizontal , vertical counters both sums 0, corner count 4. valid rectangle.
in second case, when horizontal , vertical counters both sums 0, corner count 6. not rectangle.
edit respond follow-up question:
it's bit tricky since appears character able move around freely. system described above becomes trivial if have game character moves across grid 1 step @ time.
the system still applies though, need @ order of "squares" being eaten. can still make same array of movement keeping track of last square hit , place had in grid.
for example, if follow pattern of first example squares eaten in following order:
- square 1: [2, 4]
- square 2: [1, 4] -> subtract last position: [1-2, 4-4] = [-1, 0] <- add movement array
- square 3: [1, 3] -> subtract last position: [1-1, 3-4] = [0, -1] <- add movement array
- square 4: [1, 2] -> subtract last position: [1-1, 2-3] = [0, -1] <- add movement array
- ...
- square 12: [3, 4] -> subtract last position: [3-4, 4-4] = [-1, 0] <- add movement array
- square 13: [2, 4] -> subtract last position: [2-3, 4-4] = [-1, 0] <- add movement array
you have same movement array in example above.
Comments
Post a Comment