cocos2d for python 实现Scroll 控制功能,滑动条

该博客介绍了如何利用cocos2d库在Python中创建Scroll控制功能,通过建立两个层级,一个用于用户视图,另一个作为滚动视图,并在滚动视图中添加响应鼠标事件的精灵。博主分享了采用回调函数来实现这一交互的编程思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注出处 http://blog.youkuaiyun.com/ssihc0 

编程思想,

建两个层,

一个为userview

另一个是scrollview

在scrollview 里建一个scroll sprite

这个sprite 可以响应 mosue

采用callback

上代码

#coding=utf-8
import sys;
import os;
sys.path.insert(0,os.path.join(os.path.dirname(__file__),'..'));
import pyglet;
path=os.path.join(os.path.dirname(__file__)) +'cocos';
sys.path.insert(0,path);
from cocos.director import director;
from cocos.scene import Scene;
from cocos.layer import Layer;
from cocos.sprite import Sprite;
from cocos.actions import *;
class Scroll_sprite(Sprite):
    is_event_handler=True;
    def __init_(self,image):
        super(Scroll_sprite).__init__(image);
        self.press_button=False;
    def set_call_action(self,fun):
        self.fun=fun;
    def on_mouse_motion(self,x,y,dx,dy):
        x1=self.x-x;
        if x1<10 and x1>-10 :
           #print 'move ',self.x-x,self.y-y;
           pass

    def on_mouse_press(self,x,y,buttons,modifiers):
        print buttons
        if buttons==1:
            x1=self.x-x;
            if x1<10 and x1>-10 :
                self.y=y;
    def on_mouse_drag(self,x,y,dx,dy,button,sdf):
        self.y=y;
        self.do(CallFunc(self.fun,self));
    def on_enter(self):
        super(Scroll_sprite,self).on_enter();
        director.window.push_handlers(self.on_mouse_press);
        director.window.push_handlers(self.on_mouse_motion);
        director.window.push_handlers(self.on_mouse_drag);
    def on_exit(self):
        director.window.pop_handlers(self.on_mouse_press);
        director.window.pop_handlers(self.on_moues_motion);
        director.window.pop_handlers(self.on_moues_drag);
        super(Scroll_sprite,self).on_exit;

class Scroll(Layer):
    def __init__(self):
        super(Scroll,self).__init__();
        self.ccsprite=Scroll_sprite('Selection_004.png');
        x1,y1=director.get_window_size();
        self.ccsprite.x=x1-10;
        h=y1-self.ccsprite.height/2;
        self.ccsprite.y=h;
        #传入回调
        self.ccsprite.set_call_action(self.view_move);
        self.add(self.ccsprite);
    def set_operation(self,view):
        self.view=view;
        #回调函数
    def view_move(self,csprite):
        x1,y1=director.get_window_size()
        h=1.0- float(csprite.y/float(y1));
        self.view.y=  self.view.get_height() * h ;
class viewLayer(Layer):
    def __init__(self):
        super(viewLayer,self).__init__();
        x,y=director.get_window_size();
        x=0;
        #这里布图片
        print y-20*150+50
        for i in range(0,20):
            for i1 in range(0,7):
                self.add_card(90+i1*100,y-i*150+50  );
    def get_height(self):
        x,y=director.get_window_size();
        
        return abs(y-20*150+50); 
    def add_card(self,x1,y1):
        s1=Sprite('A_1.png');
        s1.scale=0.4;
        s1.x=x1;
        s1.y=y1;
        self.add(s1);


director.init(960,600);
scene=Scene();
scroll1=Scroll();
viewLayer1=viewLayer();
scroll1.set_operation(viewLayer1);
scene.add(scroll1);
scene.add(viewLayer1);
director.show_FPS=True;
director.run(scene);



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值