鱼C论坛

 找回密码
 立即注册
查看: 291|回复: 1

[已解决]用cocos2d如何移动棋子

[复制链接]
发表于 2024-11-7 21:38:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面代码,第一步选定棋子,第二步移动到指定位置。
运行显示:    s1.do(cocos.actions.MoveTo((100 + x2 * 160, 220 + y2 * 160), duration=2))
NameError: name 's1' is not defined


  1. import pyglet
  2. import cocos
  3. from pyglet.gl import *
  4. from cocos.sprite import Sprite
  5. from cocos.scene import Scene
  6. from cocos.director import director
  7. def game_scene():
  8.     scene = Scene()
  9.     scene.add(GameLayer(), z=1)
  10.     return scene

  11. class GameLayer(cocos.layer.ColorLayer):
  12.     def __init__(self):
  13.         super().__init__(128, 128, 128, 128)
  14.         cocos.layer.Layer.is_event_handler = True
  15.         self.first=True
  16.         s1 = Sprite('red1.png')
  17.         s1.position=100,220
  18.         self.add(s1)

  19.     def on_mouse_press(self,x, y, button, modifiers):
  20.         if button == pyglet.window.mouse.LEFT:
  21.             x = round((x - 100) / 160)
  22.             y = round((y - 220) / 160)
  23.             if self.first:
  24.                 x1 = x
  25.                 y1 = y
  26.                 self.first = False

  27.             else:
  28.                 x2 = x
  29.                 y2 = y
  30.                 s1.do(cocos.actions.MoveTo((100 + x2 * 160, 220 + y2 * 160), duration=2))
  31.                 self.first = True

  32. def run_game():
  33.     director.init(resizable=True, width=520, height=640, caption="szq")
  34.     director.run(game_scene())

  35. if __name__ == "__main__":
  36.     run_game()
复制代码
最佳答案
2024-11-7 21:39:01
在您的代码中,`s1` 是在 `__init__` 方法内部定义的局部变量,因此在 `on_mouse_press` 方法中是无法访问的。您需要将 `s1` 定义为实例变量,这样可以在类的其他方法中访问它。可以通过将 `s1` 前面加上 `self.` 来实现。

以下是修改后的代码:

  1. import pyglet
  2. import cocos
  3. from pyglet.gl import *
  4. from cocos.sprite import Sprite
  5. from cocos.scene import Scene
  6. from cocos.director import director

  7. def game_scene():
  8.     scene = Scene()
  9.     scene.add(GameLayer(), z=1)
  10.     return scene

  11. class GameLayer(cocos.layer.ColorLayer):
  12.     def __init__(self):
  13.         super().__init__(128, 128, 128, 128)
  14.         cocos.layer.Layer.is_event_handler = True
  15.         self.first = True
  16.         
  17.         # 将 s1 定义为实例变量
  18.         self.s1 = Sprite('red1.png')
  19.         self.s1.position = 100, 220
  20.         self.add(self.s1)

  21.     def on_mouse_press(self,x, y, button, modifiers):
  22.         if button == pyglet.window.mouse.LEFT:
  23.             x = round((x - 100) / 160)
  24.             y = round((y - 220) / 160)
  25.             if self.first:
  26.                 self.x1 = x
  27.                 self.y1 = y
  28.                 self.first = False
  29.             else:
  30.                 x2 = x
  31.                 y2 = y
  32.                
  33.                 # 使用 self.s1 而不是 s1
  34.                 self.s1.do(cocos.actions.MoveTo((100 + x2 * 160, 220 + y2 * 160), duration=2))
  35.                 self.first = True

  36. def run_game():
  37.     director.init(resizable=True, width=520, height=640, caption="szq")
  38.     director.run(game_scene())

  39. if __name__ == "__main__":
  40.     run_game()
复制代码


现在 `s1` 被定义为 `self.s1`,这是一个实例变量,因此在 `on_mouse_press` 方法中也可以访问到它。请确保位图文件 `red1.png` 位于项目的正确

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-7 21:39:01 | 显示全部楼层    本楼为最佳答案   
在您的代码中,`s1` 是在 `__init__` 方法内部定义的局部变量,因此在 `on_mouse_press` 方法中是无法访问的。您需要将 `s1` 定义为实例变量,这样可以在类的其他方法中访问它。可以通过将 `s1` 前面加上 `self.` 来实现。

以下是修改后的代码:

  1. import pyglet
  2. import cocos
  3. from pyglet.gl import *
  4. from cocos.sprite import Sprite
  5. from cocos.scene import Scene
  6. from cocos.director import director

  7. def game_scene():
  8.     scene = Scene()
  9.     scene.add(GameLayer(), z=1)
  10.     return scene

  11. class GameLayer(cocos.layer.ColorLayer):
  12.     def __init__(self):
  13.         super().__init__(128, 128, 128, 128)
  14.         cocos.layer.Layer.is_event_handler = True
  15.         self.first = True
  16.         
  17.         # 将 s1 定义为实例变量
  18.         self.s1 = Sprite('red1.png')
  19.         self.s1.position = 100, 220
  20.         self.add(self.s1)

  21.     def on_mouse_press(self,x, y, button, modifiers):
  22.         if button == pyglet.window.mouse.LEFT:
  23.             x = round((x - 100) / 160)
  24.             y = round((y - 220) / 160)
  25.             if self.first:
  26.                 self.x1 = x
  27.                 self.y1 = y
  28.                 self.first = False
  29.             else:
  30.                 x2 = x
  31.                 y2 = y
  32.                
  33.                 # 使用 self.s1 而不是 s1
  34.                 self.s1.do(cocos.actions.MoveTo((100 + x2 * 160, 220 + y2 * 160), duration=2))
  35.                 self.first = True

  36. def run_game():
  37.     director.init(resizable=True, width=520, height=640, caption="szq")
  38.     director.run(game_scene())

  39. if __name__ == "__main__":
  40.     run_game()
复制代码


现在 `s1` 被定义为 `self.s1`,这是一个实例变量,因此在 `on_mouse_press` 方法中也可以访问到它。请确保位图文件 `red1.png` 位于项目的正确

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 16:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表