Home Web Front-end JS Tutorial Use ActionScript-like syntax to write html5 - Part 3, mouse events and game character movement

Use ActionScript-like syntax to write html5 - Part 3, mouse events and game character movement

Jan 17, 2017 pm 04:35 PM
html5 mouse events

Part 3, Mouse Events and Game Character Movement

1, Assumption
Assume that all objects that can add mouse events have a mouseEvent method, and the added mouse events come through this mouseEvent transfer.
In this case, to add a mouse event, you only need to add a mouse event to the canvas, and then loop the childList in the LGlobal class, that is, loop all the visual objects. If a mouse event is added, then call its corresponding method. .
Second, implement
1, add the mouseEvent method to the LGlobal class, and then modify the setCanvas of the LGlobal class to implement the addition and calling of canvas mouse events

LGlobal.setCanvas = function (id,width,height){  
    LGlobal.canvasObj = document.getElementById(id);  
    if(width)LGlobal.canvasObj.width = width;  
    if(height)LGlobal.canvasObj.height = height;  
    LGlobal.width = LGlobal.canvasObj.width;  
    LGlobal.height = LGlobal.canvasObj.height;  
    LGlobal.canvas = LGlobal.canvasObj.getContext("2d");  
      
    LEvent.addEventListener(LGlobal.canvasObj,LMouseEvent.MOUSE_DOWN,function(event){  
        LGlobal.mouseEvent(event,LMouseEvent.MOUSE_DOWN);  
    });  
}   
LGlobal.mouseEvent = function(event,type){  
    var key;  
    for(key in LGlobal.childList){  
        if(LGlobal.childList[key].mouseEvent){  
            LGlobal.childList[key].mouseEvent(event,type);  
        }  
    }  
}
Copy after login

2, add the mouseList array to the LSprite class, Used to save the added mouse event, and then add the mouseEvent method
In the mouseEvent method, we need to do 2 processes,
1) to determine whether we have added the mouse event. If not, loop its childList
2), if you add a mouse event to determine whether it is clicked, although LSprite is a visible class in the sense, it is currently invisible. What is visible is the Bitmap on it. To be precise, it is The BitmapData in this Bitmap class, to be more precise, is the Image in this BitmapData, so to determine whether it has been clicked, you need to determine whether the visual object in the childList in the LSprite has been clicked. If it is clicked, call the corresponding The method

mouseEvent:function (event,type,cood){  
        if(cood==null)cood={x:0,y:0};  
        var self = this;  
        if(self.mouseList.length == 0){  
            for(key in self.childList){  
                if(self.childList[key].mouseEvent){  
                    self.childList[key].mouseEvent(event,type,{x:self.x+cood.x,y:self.y+cood.y});  
                }  
            }  
            return;  
        }  
        if(self.childList.length == 0)return;  
        var key;  
        var isclick = false;  
        for(key in self.childList){  
            isclick = self.childList[key].ismouseon(event,{x:self.x+cood.x,y:self.y+cood.y});  
            if(isclick)break;  
        }  
        if(isclick){  
            for(key in self.mouseList){  
                var obj = self.mouseList[key];  
                if(obj.type == type){  
                    event.selfX = event.offsetX - (self.x+cood.x);  
                    event.selfY = event.offsetY - (self.y+cood.y);  
                    event.currentTarget = self;  
                    obj.listener(event);  
                }  
            }  
            return;  
        }  
          
    },  
    ismouseon:function(event,cood){  
        var self = this;  
        var key;  
        var isclick = false;  
        for(key in self.childList){  
            isclick = self.childList[key].ismouseon(event,{x:self.x+cood.x,y:self.y+cood.y});  
            if(isclick)break;  
        }  
        return isclick;  
    }
Copy after login

ismouseon method is used to determine whether it is clicked
The LBitmap class also needs to determine whether it is clicked, so add ismouseon

ismouseon:function(event,cood){  
        var self = this;  
        if(event.offsetX >= self.x + cood.x && event.offsetX <= self.x + cood.x + self.width &&   
            event.offsetY >= self.y + cood.y && event.offsetY <= self.y + cood.y + self.height){  
            return true;  
        }else{  
            return false;  
        }  
    }
Copy after login

When adding mouse events, imitate ActionScript syntax

backLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown);
Copy after login

Next, prepare a map, a character walking map, and use mouse events to control the character's movement.

init(80,"back",800,480,main);  
  
  
var list = new Array();  
var index = 0;  
var backLayer;  
//地图  
var mapimg;  
//人物  
var playerimg;  
var loader  
var imageArray;  
var animeIndex = 0;  
var dirindex = 0;  
var dirarr = new Array({x:0,y:1},{x:-1,y:0},{x:1,y:0},{x:0,y:-1},{x:-1,y:1},{x:1,y:1},{x:-1,y:-1},{x:1,y:-1});  
var dirmark = {"0,1":0,"-1,0":1,"1,0":2,"0,-1":3,"-1,1":4,"1,1":5,"-1,-1":6,"1,-1":7};  
  
  
//移动目标  
var toX = 0;  
var toY = 0;  
function main(){  
      
    loader = new LLoader();  
    loader.addEventListener(LEvent.COMPLETE,loadBitmapdata);  
    loader.load("back.jpg","bitmapData");  
}  
function loadBitmapdata(event){  
    var bitmapdata = new LBitmapData(loader.content);  
    mapimg = new LBitmap(bitmapdata);  
    loader = new LLoader();  
    loader.addEventListener(LEvent.COMPLETE,loadOver);  
    loader.load("1.png","bitmapData");  
}  
function loadOver(event){  
    var bitmapdata = new LBitmapData(loader.content,0,0,70,92);  
    imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,8,8);  
    document.getElementById("inittxt").innerHTML="";  
    playerimg = new LBitmap(bitmapdata);  
    playerimg.bitmapData.setCoordinate(0,0);  
    index = 0;  
    backLayer = new LSprite();  
    addChild(backLayer);  
    backLayer.addChild(mapimg);  
    backLayer.addChild(playerimg);  
    backLayer.addEventListener(LEvent.ENTER_FRAME, onframe)  
    backLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown);  
}  
  
  
function onframe(){  
    index++;  
    if(index >= imageArray[0].length){  
        index = 0;  
    }  
    var markx = 0,marky = 0;  
    var l = 3;  
    if(playerimg.x > toX){  
        playerimg.x -= l;  
        markx = -1;  
    }else if(playerimg.x < toX){  
        playerimg.x += l;  
        markx = 1;  
    }  
    if(playerimg.y > toY){  
        playerimg.y -= l;  
        marky = -1;  
    }else if(playerimg.y < toY){  
        playerimg.y += l;  
        marky = 1;  
    }  
    if(markx !=0 || marky != 0){  
        var mark = markx+","+marky;  
        dirindex = dirmark[mark];  
    }  
    playerimg.bitmapData.setCoordinate(imageArray[dirindex][index].x,imageArray[dirindex][index].y);  
}  
function onmousedown(event){  
    toX = parseInt(event.selfX/3)*3;  
    toY = parseInt(event.selfY/3)*3;  
}
Copy after login

The above is to use ActionScript-like syntax to write html5—— The third article covers mouse events and game character movement. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles