class Main extends egret.DisplayObjectContainer { /** * 入口文件, 最先执行的构造方法 * 这会实例化一个和手机屏幕一样大的舞台 */ public constructor() { super(); this.once( egret.Event.ADDED_TO_STAGE, this.begin, this ); } /** * 入口文件加载成功后执行的方法 * 也是逻辑的开始, 通过触摸移动显示对象 */ private begin(event:egret.Event) { /** * 创建一个文本框,设定一个 scrollRect 限制显示范围 * text: 文字内容 * scrollRect: 显示范围 * cacheAsBitmap: 是否缓存显示对象 */ var bigText: egret.TextField = new egret.TextField(); bigText.text = "平移和滚动显示对象,平移和滚动显示对象"; bigText.scrollRect = new egret.Rectangle(0, 0, 640, 50); bigText.cacheAsBitmap = true; bigText.x = 0; bigText.y = 10; this.addChild(bigText); /** * 创建一个左移, 一个右移 按钮 * ev.currentTarget 获取触发事件的对象名 */ var btnLeft: egret.Shape = new egret.Shape(); btnLeft.graphics.beginFill(0xcccc01); btnLeft.graphics.drawRect(0, 0, 50, 50); btnLeft.graphics.endFill(); btnLeft.x = 50; btnLeft.y = 100; this.addChild(btnLeft); btnLeft.touchEnabled = true; btnLeft.addEventListener(egret.TouchEvent.TOUCH_TAP, onScroll, this); var btnRight: egret.Shape = new egret.Shape(); btnRight.graphics.beginFill(0x01cccc); btnRight.graphics.drawRect(0,0,50,50); btnRight.graphics.endFill(); btnRight.x = 150; btnRight.y = 100; this.addChild(btnRight); btnRight.touchEnabled = true; btnRight.addEventListener(egret.TouchEvent.TOUCH_TAP, onScroll, this); function onScroll(ev: egret.TouchEvent): void { var rect: egret.Rectangle = bigText.scrollRect; switch (ev.currentTarget) { case btnLeft: rect.x += 20; break; case btnRight: rect.x -= 20; break; } bigText.scrollRect = rect; } } }
时间: 2025-01-07 15:28:35