cocos2d-地圖滾動

來源:互聯網
上載者:User

地圖滾動可以使用如下模組實現

Module cocos.layer.scrolling

通過ScrollingManager的set_focus()來設定焦點的位置。

需要給ScrollableLayer定義.px_width和.px_height兩個屬性

這樣Manager就用這兩個值來顯示畫面。

詳細情況參照下面代碼中注釋

程式碼範例(內建代碼的簡化版test_scrolling_manager_without_tiles.py)

#-*- encoding:utf-8 -*-import cocosfrom pyglet.window import keyimport cocos.euclid as euclass SquareLand(cocos.layer.ScrollableLayer):    is_event_handler = True    def __init__(self):        super(SquareLand, self).__init__()        #定義世界地圖的寬和高        self.px_width = 1000 + 4*98 #1392        self.px_height = 1000        #為了縮短後面代碼的長度        px_width = self.px_width        px_height = self.px_height        #下面的代碼中定義了兩個圖層,一個大的一個稍微小的        #這兩個背景重疊起來就形成了一個帶有邊框的地圖效果        bg = cocos.layer.ColorLayer(170,170,0,255,width=px_width,                                    height=px_height)        self.add(bg, z=0)                margin = int(px_width*0.01)        #print 'margin',margin        self.margin = margin        bg = cocos.layer.ColorLayer(0,170,170,255,width=px_width-2*margin,                                    height=px_height-2*margin)        bg.position = (margin,margin)        self.add(bg, z=1)                    #定義使用者精靈        self.player = cocos.sprite.Sprite("grossinis_sister1.png")        self.player.position = (self.player.width/2,self.player.height/2)        self.player.fastness = 200                self.add(self.player, z=4)        #這裡定義一個字典,來表示按鈕是否別按下        self.buttons = { #button state : current value, 0 not pressed, 1 pressed            key.LEFT:0,            key.RIGHT:0,            key.UP:0,            key.DOWN:0            }        #每一幀都會更新player的狀態,具體參照step函數        self.schedule(self.step)    def on_enter(self):        #擷取scroller的指標,用來設定set_focus,注意要調用父類的on_enter        super(SquareLand,self).on_enter()        self.scroller = self.get_ancestor(cocos.layer.ScrollingManager)            def on_key_press(self,k,modifiers):        #請注意這裡的小技巧        if k in self.buttons:            self.buttons[k] = 1                def on_key_release(self, k, modifiers):        if k in self.buttons:            self.buttons[k] = 0                #用來判斷是否到達地圖的邊緣,如果到達邊緣則不會繼續移動    def clamp(self, actor, new_pos):        x,y = new_pos        if  x-actor.width/2<self.margin:            x = self.margin + actor.width/2        elif x+actor.width/2>self.px_width-self.margin:            x = self.px_width-self.margin-actor.width/2        if  y-actor.height/2<self.margin:            y = self.margin + actor.height/2        elif y+actor.height/2>self.px_height-self.margin:            y = self.px_height-self.margin-actor.height/2        return x,y        def step( self, dt ):        btns = self.buttons        #請注意這裡用來移動的小技巧        move_dir = eu.Vector2(btns[key.RIGHT]-btns[key.LEFT],                              btns[key.UP]-btns[key.DOWN])        changed = False        if move_dir:            new_pos = self.player.position + self.player.fastness*dt*move_dir.normalize()            new_pos = self.clamp(self.player, new_pos)            self.player.position = new_pos            changed = True        if changed:            self.update_after_change()    def update_after_change(self):        self.scroller.set_focus(*self.player.position) if __name__ == '__main__':    cocos.director.director.init(1024,768)#view_rectangle    scene = cocos.scene.Scene()    world_layer = SquareLand()    scroller = cocos.layer.ScrollingManager()    scroller.add(world_layer)    scene.add(scroller)    cocos.director.director.run(scene)

《本節完》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.