在JavaScript中使用箭头键移动图像

时间:2018-04-02 15:59:11

标签: javascript html

我正在为一个学校项目制作一个简单的Javascript游戏。我正在尝试用图像替换红色方块(在以下链接中)。

enter link description here 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:-1)

好问题!使用代码段发布您的代码。此链接应提供完整的答案add image



PdfReader reader = new PdfReader(src);
//PdfReader reader = new PdfReader(src);
//PdfStamper stamper = new PdfStamper(reader, new FileStream(src, FileMode.Create));

AcroFields form = reader.AcroFields;

foreach(String key in form.Fields.Keys){
    int type = form.GetFieldType(key);
    switch (type)
    {
        case AcroFields.FIELD_TYPE_CHECKBOX:
            Console.WriteLine("Checkbox");
            break;
       ........
..........

var myGamePiece;

function startGame() {
    myGamePiece = new component(30, 30, "red", 10, 120);
    myGameArea.start();
}

var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 480;
        this.canvas.height = 270;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
    },
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

function component(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;    
    this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;        
    }    
}

function updateGameArea() {
    myGameArea.clear();
    myGamePiece.newPos();    
    myGamePiece.update();
}

function moveup() {
    myGamePiece.speedY -= 1; 
}

function movedown() {
    myGamePiece.speedY += 1; 
}

function moveleft() {
    myGamePiece.speedX -= 1; 
}

function moveright() {
    myGamePiece.speedX += 1; 
}

canvas {
    border:1px solid #d3d3d3;
    background-color: #f1f1f1;
}