Commit 2aee812d by Shawn Wang

socket and control

parent 71d6bd0d
class Controller extends egret.Sprite {
public constructor() {
super();
this.init()
}
private speed = 10
private speedX = 0
private speedY = 0
private p1:egret.Point = new egret.Point();
private p2:egret.Point = new egret.Point();
private control = new egret.Shape();
private pointtt = new egret.Shape();
private controlCenter = new egret.Point(1100, 450)
// 初始化(给开始按钮绑定点击事件)
private init() {
this.control.graphics.beginFill( 0xFF3866, 1);
this.control.graphics.drawCircle(1100, 450, 90);
this.control.graphics.endFill();
this.addChild( this.control );
this.pointtt.graphics.beginFill(0xFF0000, 1)
this.pointtt.graphics.drawCircle(1100, 450, 10)
this.pointtt.graphics.endFill()
this.addChild(this.pointtt)
this.touchEnabled = true
this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.controlTouchBegin, this)
this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.controlTouchMove, this)
this.addEventListener(egret.TouchEvent.TOUCH_END, this.controlTouchEnd, this)
this.addEventListener(egret.Event.ENTER_FRAME,this.onEnterFrame,this)
}
private onEnterFrame(e: egret.Event){
if(this.touchEnd){
return
}
this.pointtt.x += this.speedX
this.pointtt.y += this.speedY
}
private touchEnd = false
private controlTouchBegin(e: egret.TouchEvent){
this.touchEnd = false
}
private controlTouchMove(e: egret.TouchEvent){
var angle = Math.atan2(e.stageY - this.controlCenter.y, e.stageX - this.controlCenter.x )
// console.error(e)
this.speedX = Math.cos(angle)*this.speed;
this.speedY = Math.sin(angle)*this.speed;
// console.error(this.speedX)
// console.error(e.stageX)
}
private controlTouchEnd(e: egret.TouchEvent){
this.touchEnd = true
// this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
}
public release() {
// 移除事件
}
}
\ No newline at end of file
...@@ -55,19 +55,62 @@ class Main extends egret.DisplayObjectContainer { ...@@ -55,19 +55,62 @@ class Main extends egret.DisplayObjectContainer {
this.runGame().catch(e => { this.runGame().catch(e => {
console.log(e); console.log(e);
}) })
} }
private async runGame() { private async runGame() {
await this.loadResource() await this.loadResource()
var stage = egret.MainContext.instance.stage;
stage.setContentSize(1334, 750);
this.createGameScene(); this.createGameScene();
const result = await RES.getResAsync("description_json")
await platform.login(); await platform.login();
const userInfo = await platform.getUserInfo(); const userInfo = await platform.getUserInfo();
console.log(userInfo); console.log(userInfo);
platform.connectSocket({
url: "ws://192.168.50.118:9090",
header: {
'content-type': 'application/json'
},
method: "GET",
success: (...args) => {
console.error("success")
console.error(args)
},
fail: (...args) => {
console.error("fail")
console.error(args)
}
})
platform.onSocketOpen(() => {
platform.sendSocketMessage({
data: JSON.stringify({
cid: "asnbduawh",
seq: "moving",
x: 100,
y: 123
}),
success: () => {
console.error("sendSocketMessage success")
},
fail: () => {
console.error("sendSocketMessage fail")
}
})
})
platform.onSocketMessage(function(res) {
console.log('收到服务器内容:' + res.data)
})
platform.onSocketError((e) => {
console.error("onSocketError")
console.error(e)
})
platform.onSocketClose((e) => {
console.error("onSocketClose")
console.error(e)
})
} }
private async loadResource() { private async loadResource() {
...@@ -83,6 +126,9 @@ class Main extends egret.DisplayObjectContainer { ...@@ -83,6 +126,9 @@ class Main extends egret.DisplayObjectContainer {
} }
} }
private textfield: egret.TextField;
private myPosition = new egret.Point(0, 0)
/** /**
* 创建游戏场景 * 创建游戏场景
* Create a game scene * Create a game scene
...@@ -91,4 +137,15 @@ class Main extends egret.DisplayObjectContainer { ...@@ -91,4 +137,15 @@ class Main extends egret.DisplayObjectContainer {
this.addChild(SceneManager.getInstance()); this.addChild(SceneManager.getInstance());
} }
/**
* 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
* Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
*/
private createBitmapByName(name: string) {
let result = new egret.Bitmap();
let texture: egret.Texture = RES.getRes(name);
result.texture = texture;
return result;
}
} }
...@@ -10,13 +10,51 @@ declare interface Platform { ...@@ -10,13 +10,51 @@ declare interface Platform {
login(): Promise<any> login(): Promise<any>
connectSocket(options): void
onSocketOpen(callback): void
onSocketClose(callback): void
onSocketMessage(callback): void
onSocketError(callback): void
sendSocketMessage(callback): void
request(options): void
} }
class DebugPlatform implements Platform { class DebugPlatform implements Platform {
async getUserInfo() { async getUserInfo() {
return { nickName: "username" } return { nickName: "username" }
} }
async login() { async login() {
}
showToast(title){
}
connectSocket(options){
}
onSocketOpen(callback){
}
onSocketClose(callback){
}
onSocketMessage(options){
}
onSocketError(callback){
}
sendSocketMessage(callback){
}
request(){
} }
} }
......
class GameScene extends egret.Sprite { class GameScene extends egret.Sprite {
private controller
public constructor() { public constructor() {
super(); super();
this.init() this.init()
...@@ -8,6 +10,8 @@ class GameScene extends egret.Sprite { ...@@ -8,6 +10,8 @@ class GameScene extends egret.Sprite {
// 初始化(给开始按钮绑定点击事件) // 初始化(给开始按钮绑定点击事件)
private init() { private init() {
// put display object to this scene // put display object to this scene
this.controller = new Controller()
this.addChild(this.controller)
} }
public release() { public release() {
......
...@@ -31,6 +31,18 @@ egret_native.egretStart = function () { ...@@ -31,6 +31,18 @@ egret_native.egretStart = function () {
//以下为自动修改,请勿修改 //以下为自动修改,请勿修改
//The following is automatically modified, please do not modify //The following is automatically modified, please do not modify
//----auto option start---- //----auto option start----
entryClassName: "Main",
frameRate: 30,
scaleMode: "fixedWidth",
contentWidth: 640,
contentHeight: 1136,
showPaintRect: false,
showFPS: false,
fpsStyles: "x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9",
showLog: false,
logFilter: "",
maxTouches: 2,
textureScaleFactor: 1
//----auto option end---- //----auto option end----
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment