Quick Cocos Naja

Source: Internet
Author: User

LocalScheduler =require(CC. Package_name.". Scheduler")LocalMainscene = Class ("Mainscene",function()    returnDisplay.newscene ("Mainscene")End)--equivalent to constructors in C + +functionMainscene:ctor ()--sets the background to white, and this is the layer of the current scene so that touch events can be implemented laterSelf.touchlayer_ = Display.newcolorlayer (cc.c4b (255,255,255,255): AddTo (self)--Add Ninja ElfSelf.player_ = Display.newsprite ("Player.png"): AddTo (Self.touchlayer_) Self.player_:pos (Self.player_:getContentSize (). Width/2, Display.height/2)    --initialize an array of storage darts and enemies (monsters)Self.projectiles_ ={} Self.monsters_= {}    --to add a method for each frame refreshSelf:addnodeeventlistener (CC. Node_enter_frame_event,handler (self, self.update)) self:scheduleupdate ()--Add a method to refresh per secondScheduler.scheduleglobal (function() Self:addmonster ()--methods to invoke per second    End,1)--1 for Time    --Adding Touch EventsSelf.touchLayer_:setTouchEnabled (true) Self.touchLayer_:addNodeEventListener (CC. Node_touch_event,function(Event)ifEvent.name = ="ended"  Thenself:ontouchended (EVENT.X,EVENT.Y)End        return true    End)End--Add EnemiesfunctionMainscene:addmonster ()LocalMonster = Display.newsprite ("Monster.png"): AddTo (Self.touchlayer_)LocalMiny = Monster:getcontentsize (). Height/2    LocalMaxy = Display.height-monster:getcontentsize (). Height/2    LocalRangey = Maxy-MinyLocalActualy =Math.random(Rangey) monster:pos (Display.width+ monster:getcontentsize (). Width/2, actualy)LocalMinduration =2.0    LocalMaxduration =4.0    LocalRangeduration = maxduration-mindurationLocalActualDuration = (Math.random(rangeduration)) Transition.moveto (monster,{x=-monster:getcontentsize (). Width/2, y=actualy, time=actualduration, OnComplete=function(Event)--Delete after execution endsSelf.monsters_[event] =Nilevent:removeself ()End    })    --add data to the monster to deleteSelf.monsters_[monster] =MonsterEndfunctionmainscene:ontouchended (x, y)Print("ontouchended")    LocalLocation =CC.P (x, y)LocalProjectile = Display.newsprite ("Projectile.png"): AddTo (self) projectile:pos ( -, Display.height/2)    Localpx, py =projectile:getposition ()Localoffset =cc.psub (LOCATION,CC.P (px,py))if(Offset.x <=0) Then return End    LocalRealx = Display.width + projectile:getcontentsize (). Width/2    LocalRatio = OFFSET.Y/Offset.xLocalrealy = Realx * ratio +pyLocalRealdest =CC.P (realx,realy)LocalOffrealx = Realx-pxLocalOffrealy = realy-pyLocalLength =math.sqrt(OFFREALX * offrealx + offrealy *offrealy)LocalVelocity =480/1    LocalRealmoveduration = length/Velocity Transition.moveto (projectile,{x=realdest.x, y=Realdest.y, time=realmoveduration, OnComplete=function(event) self.projectiles_[event]=Nilevent:removeself ()End}) Self.projectiles_[projectile]=ProjectileEnd--ask for distance between two pointsLocal functionDist (ax, Ay, bx, by)LocalDX, dy = bx-ax, by-ayreturn math.sqrt(DX * dx + dy *dy)End--Refresh per framefunctionmainscene:update (DT)--Print ("Update")    LocalProjectilestodelete = {}     forK_pro, V_proinch Pairs(Self.projectiles_) Do        LocalMonsterstodelete = {}         forK_mon, V_moninch Pairs(Self.monsters_) Do            Localp_x, p_y =v_pro:getposition ()LocalM_x,m_y =v_mon:getposition ()ifDist (p_x,p_y,m_x,m_y) <= -   ThenMonsterstodelete[v_mon]=V_mon Projectilestodelete[v_pro]=V_proEnd        End         forK_m_d, V_m_dinch Pairs(Monsterstodelete) Do            --local x, y = v_m_d:getposition ()Self.monsters_[v_m_d] =Nilv_m_d:removeself ()EndMonsterstodelete=Nil    End     forK_p_d, V_p_dinch Pairs(Projectilestodelete) DoSelf.projectiles_[v_p_d]=Nilv_p_d:removeself ()EndProjectilestodelete=NilEndfunctionMainscene:onenter ()Endfunctionmainscene:onexit ()EndreturnMainscene

Quick Cocos Naja

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.