2015-07-20 67 views
0

我正在基於cocos2d編程指南中的一些introductory lessons(適用於2D遊戲設計)開發以下程序。如何在cocos2d(python)中創建圖像的按鍵觸發器移動?

該程序有兩個類,HelloWorld和KeyDisplay。

HelloWorld顯示一隻鳥的圖像和短語「Get this bird some pizza!」。該短語每2秒使用cocos.actions.ScaleBycocos.actions.Reverse放大和縮小。它效果很好。

KeyDisplay使用用戶鍵盤輸入並使用on_key_press函數將字符寫入屏幕。它也正常工作。

接下來我想通過KeyDisplay修改HelloWorld中正在進行的操作。例如,我想更改圖像的比例(sprite.scale)或更改圖像的位置(sprite.position)。這需要HelloWorld和KeyDisplay類對象相互交談。

不幸的是,KeyDisplay類不知道HelloWorld類,因此變量不可訪問。例如,如果我嘗試在on_key_press函數中設置sprite.scale = 0.5,則會出現錯誤。

KeyDisplay是否有辦法響應按鍵事件,修改我的HelloWorld類中的精靈和其他變量?我猜這是一個python編程問題和一個cocos2d遊戲設計問題。通常我需要在HelloWorld類中創建精靈和標籤(在__init__(self)方法中)。到目前爲止,我還沒有被允許在全球範圍內創建精靈或標籤。感謝您的任何建議。

import cocos 
import pyglet 
# note: needed to change something in the operating system before importing cocos, running this command: 
# defaults write org.python.python ApplePersistenceIgnoreState NO 



class HelloWorld(cocos.layer.ColorLayer): 
    def __init__(self): 
     super(HelloWorld, self).__init__(64,64,224,255) 
     self.world_width = 1000 
     self.world_height = 1000 
     self.px_width = 1000 
     self.px_height = 1000 

     label = cocos.text.Label('Get this bird some pizza!', 
         font_name='Times New Roman', 
         font_size=32, 
         anchor_x='center', anchor_y='center') 

     label.position = 320,100 
     self.add(label) 
     sprite = cocos.sprite.Sprite('bower-bird.jpeg') 
     sprite.position = 320,320 
     sprite.scale = 1 
     self.add(sprite, z=1) 

     scale = cocos.actions.ScaleBy(3, duration=2) 
     label.do(cocos.actions.Repeat(scale + cocos.actions.Reverse(scale))) 
     #sprite.do(cocos.actions.Repeat(scale + cocos.actions.Reverse(scale))) 


class KeyDisplay(cocos.layer.Layer): 

    # If you want that your layer receives director.window events 
    # you must set this variable to 'True' 
    is_event_handler = True 

    def __init__(self): 

     super(KeyDisplay, self).__init__() 

     self.text = cocos.text.Label("", x=100, y=280) 

     # To keep track of which keys are pressed: 
     self.keys_pressed = set() 
     self.update_text() 
     self.add(self.text) 

    def update_text(self): 
     key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed] 
     text = 'Keys: '+','.join (key_names) 
     # Update self.text 
     self.text.element.text = text 

    def on_key_press (self, key, modifiers): 
     """This function is called when a key is pressed. 
     'key' is a constant indicating which key was pressed. 
     'modifiers' is a bitwise or of several constants indicating which 
      modifiers are active at the time of the press (ctrl, shift, capslock, etc.) 
     """ 
     self.keys_pressed.add (key) 
     self.update_text() 

    def on_key_release (self, key, modifiers): 
     """This function is called when a key is released. 

     'key' is a constant indicating which key was pressed. 
     'modifiers' is a bitwise or of several constants indicating which 
      modifiers are active at the time of the press (ctrl, shift, capslock, etc.) 

     Constants are the ones from pyglet.window.key 
     """ 

     self.keys_pressed.remove (key) 
     self.update_text() 

    def update_text(self): 
     key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed] 
     text = 'Keys: '+','.join (key_names) 
     # Update self.text 
     self.text.element.text = text 

#start it up ... 

cocos.director.director.init() 
hello_layer = HelloWorld() 
main_scene = cocos.scene.Scene (hello_layer, KeyDisplay()) 
cocos.director.director.run (main_scene) 

回答

0

這是gituser一個例子:JPwright 基本上創建一個移動類(「我」類),那麼你調用它在你的精靈的「做」的功能。 'step'功能基本上是「完成」的功能

import pyglet 
from pyglet.window import key 

import cocos 
from cocos import actions, layer, sprite, scene 
from cocos.director import director 

# Player class 

class Me(actions.Move): 

    # step() is called every frame. 
    # dt is the number of seconds elapsed since the last call. 
    def step(self, dt): 

    super(Me, self).step(dt) # Run step function on the parent class. 

    # Determine velocity based on keyboard inputs. 
    velocity_x = 100 * (keyboard[key.RIGHT] - keyboard[key.LEFT]) 
    velocity_y = 100 * (keyboard[key.UP] - keyboard[key.DOWN]) 

    # Set the object's velocity. 
    self.target.velocity = (velocity_x, velocity_y) 

# Main class 

def main(): 
    global keyboard # Declare this as global so it can be accessed within class methods. 

    # Initialize the window. 
    director.init(width=500, height=300, autoscale=False, resizable=True) 

    # Create a layer and add a sprite to it. 
    player_layer = layer.Layer() 
    me = sprite.Sprite('bower-bird.jpeg') 
    player_layer.add(me) 

    # Set initial position and velocity. 
    me.position = (100, 100) 
    me.velocity = (0, 0) 

    # Set the sprite's movement class. 
    me.do(Me()) 

    # Create a scene and set its initial layer. 
    main_scene = scene.Scene(player_layer) 

    # Attach a KeyStateHandler to the keyboard object. 
    keyboard = key.KeyStateHandler() 
    director.window.push_handlers(keyboard) 

    # Play the scene in the window. 
    director.run(main_scene) 

if __name__ == '__main__': 
    main()