小编Jak*_*ake的帖子

如何使用键盘输入顺利移动JS对象?

所以我是HTML5的新手,并决定开始编写乒乓球游戏.我想用键盘顺利移动我的角色,左边的W和S键以及右边的向上和向下箭头.我似乎无法让它发挥作用.这可能很容易,但我是我的菜鸟,我需要一些帮助.提前致谢!

<!DOCTYPE html>
<html>
  <head>
      <title>Pingedy Pong</title>
  </head>
  <body style="font-family:Arial;">
      <canvas id="ctx" width="699" height="400" style="background-color:black;border:2px solid black;background-image: url('background.jpg');"></canvas>
      <script>
        
        // STARTING SCRIPT
          
        var ctx = document.getElementById("ctx").getContext("2d");
        ctx.fillStyle="white";
        ctx.font = '30px Arial';
        
        // VARIABLES
          
        var keys1 = [];
        var keys2 = [];
          
        var width = 699;
        var height = 400;
          
        var ball = {
            width:20,
            height:20,
            spdX:2.9,
            spdY:2.9,
            x:340,
            y:190,
        };
          
        var char1 = {
            w:15,
            h:90,
            spdX:3,
            spdY:3,
            x:10,
            y:155,
        };
          
        var char2 = {
            w:15,
            h:90,
            spdX:3,
            spdY:3,
            x:674, …
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery html5

5
推荐指数
1
解决办法
1140
查看次数

构造函数导致无限循环

我正在尝试完成这个PHP挑战,我目前有这个代码:

<?php

/*
Challenge 2: Implement AnswerInterface and get Question to echo "4".
*/  
class Question
{
    public function __construct(AnswerInterface $answer)
    { 
        echo "What is 2 + 2?\n";
        $answer = $answer->get()->the()->answer();
        if ($answer instanceof AnswerInterface) {
            echo $answer . PHP_EOL;
        }
    }   
}   

interface AnswerInterface
{   
    public function get();
    public function the();
    public function answer();
}   

class Answer implements AnswerInterface
{   
    public function get()
    {   
        return new Answer();
    }

    public function the()
    {
        return new Answer();
    }

    public …
Run Code Online (Sandbox Code Playgroud)

php oop

1
推荐指数
1
解决办法
100
查看次数

标签 统计

css ×1

html ×1

html5 ×1

javascript ×1

jquery ×1

oop ×1

php ×1