Home  >  Article  >  Web Front-end  >  Use ActionScript-like syntax to write html5 - Part 4, inheritance and simple rpg

Use ActionScript-like syntax to write html5 - Part 4, inheritance and simple rpg

黄舟
黄舟Original
2017-01-17 16:39:351441browse

This time I use a class inherited from LSprite to implement a simple rpg demo
Let’s first take a look at the similarity between the final code and as

var backLayer;  
//地图  
var mapimg;  
//人物  
var playerimg;  
var loader  
var imageArray;  
var loadIndex = 0;  
var imgData = new Array({name:"back.jpg",img:null},{name:"1.png",img:null},{name:"2.png",img:null});  
var chara;  
var charaList;  
  
  
function main(){  
    loadImage();  
}  
function loadImage(){  
    if(loadIndex >= imgData.length){  
        gameInit();  
        return;  
    }  
    loader = new LLoader();  
    loader.addEventListener(LEvent.COMPLETE,loadComplete);  
    loader.load(imgData[loadIndex].name,"bitmapData");  
}  
function loadComplete(event){  
    imgData[loadIndex].img = loader.content;  
    loadIndex++;  
    loadImage();  
}  
function gameInit(event){  
    var bitmapdata;  
    bitmapdata = new LBitmapData(imgData[0].img);  
    mapimg = new LBitmap(bitmapdata);  
      
    document.getElementById("inittxt").innerHTML="";  
    backLayer = new LSprite();  
    addChild(backLayer);  
    backLayer.addChild(mapimg);  
      
    bitmapdata = new LBitmapData(imgData[1].img,0,0,70,92);  
    imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,8,8);  
    playerimg = new LBitmap(bitmapdata);  
    chara = new CharacterSprite(true,playerimg,imageArray,0,0);  
    backLayer.addChild(chara);  
  
  
    charaList = new Array();  
    for(var i=0;i<10;i++){  
        bitmapdata = new LBitmapData(imgData[2].img,0,0,80,91);  
        imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,8,8);  
        playerimg = new LBitmap(bitmapdata);  
        var npcx = parseInt(Math.random()*800/3)*3;  
        var npcy = parseInt(Math.random()*480/3)*3;  
        var npc = new CharacterSprite(false,playerimg,imageArray,npcx,npcy);  
        backLayer.addChild(npc);  
        charaList.push(npc);  
    }  
      
    backLayer.addEventListener(LEvent.ENTER_FRAME, onframe)  
    backLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown);  
}  
  
  
function onframe(){  
    chara.onframe();  
      
    for(var i=0;i<charaList.length;i++){  
        charaList[i].onframe();  
    }  
}  
function onmousedown(event){  
    chara.toCoordinate.x = parseInt(event.selfX/3)*3;  
    chara.toCoordinate.y = parseInt(event.selfY/3)*3;  
}


It should be pretty good, right?
Take a look at the results. If you don’t see the effect, please download a browser that supports html5
http://fsanguo.comoj.com/html5/jstoas03/index.html


Below Let’s talk about how to inherit. If it is inherited, js cannot inherit like as.
JS inheritance is as follows

function base(derive,baseSprite,baseArgs){  
    baseSprite.apply(derive,baseArgs);  
      
    for(prop in baseSprite.prototype){  
        var proto = derive.constructor.prototype;  
        if(!proto[prop]){  
            proto[prop] = baseSprite.prototype[prop];  
        }  
    }  
}

The three parameters are child, base, and base constructor parameter array
This The method can realize the perfect inheritance of js
Now let’s create a class CharacterSprite that inherits from LSprite
You only need to call base(this,LSprite,[]) in the constructor to achieve inheritance
And CharacterSprite because of inheritance LSprite method, so it has addChild and other methods
CharacterSprite class code is as follows

function CharacterSprite(ishero,bitmap,imageArray,x,y){  
    base(this,LSprite,[]);  
    var self = this;  
    self.x = x;  
    self.y = y;  
    self.toCoordinate = {x:x,y:y};  
    self.ishero = ishero;  
    self.animeIndex = 0;  
    self.dirindex = 0;  
    self.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};  
      
    self.bitmap = bitmap;  
    self.imageArray = imageArray;  
    self.addChild(bitmap);  
}  
CharacterSprite.prototype.onframe = function (){  
    var self = this;  
    self.animeIndex++;  
    if(self.animeIndex >= self.imageArray[0].length){  
        self.animeIndex = 0;  
    }  
    var markx = 0,marky = 0;  
    var l = 3;  
    if(self.x > self.toCoordinate.x){  
        self.x -= l;  
        markx = -1;  
    }else if(self.x < self.toCoordinate.x){  
        self.x += l;  
        markx = 1;  
    }  
    if(self.y > self.toCoordinate.y){  
        self.y -= l;  
        marky = -1;  
    }else if(self.y < self.toCoordinate.y){  
        self.y += l;  
        marky = 1;  
    }  
    if(markx !=0 || marky != 0){  
        var mark = markx+","+marky;  
        self.dirindex = self.dirmark[mark];  
    }else if(!self.ishero){  
        if(self.index > 0){  
            self.index -= 1;  
        }else{  
            self.index = parseInt(Math.random()*300);  
            self.toCoordinate.x = parseInt(Math.random()*800/3)*3;  
            self.toCoordinate.y = parseInt(Math.random()*480/3)*3;  
        }  
    }  
    self.bitmap.bitmapData.setCoordinate(self.imageArray[self.dirindex][self.animeIndex].x,self.imageArray[self.dirindex][self.animeIndex].y);  
      
}

The above is to use ActionScript-like syntax to write html5 - Part 4, inheritance and simple rpg content, more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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