elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Convertir objeto en bola javascript
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Convertir objeto en bola javascript  (Leído 1,844 veces)
alzola22

Desconectado Desconectado

Mensajes: 15


Ver Perfil
Convertir objeto en bola javascript
« en: 10 Diciembre 2019, 10:32 am »

Hola, estoy haciendo un proyecto en javascript y querría cambiar la figura del cuadrado por una bola. el codigo con (cuadrado).
Código:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
    border:1px solid #d3d3d3;
    background-color: red;
}
</style>
</head>
<body onload="startGame()">
<script>
var myGamePiece;
function startGame() {
    myGamePiece = new component(30, 30, "yellow", 0, 0);
    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);       
    },
    stop : function() {
        clearInterval(this.interval);
    },   
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}
function component(width, height, color, x, y, type) {
    this.type = type;
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;   

    this.speedX = 1;
    this.speedY = 1;   
    this.gravity = 0;
    this.gravitySpeed = 0;
    this.bounce = 0.5;
    this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
    this.newPos = function() {
        this.gravitySpeed += this.gravity;
        this.x += this.speedX;
        this.y += this.speedY + this.gravitySpeed;
        this.hitBottom();
if(this.x > 450){
this.speedX = -this.speedX;
}
if(this.x < 0){
this.speedX = -this.speedX;
}
if(this.y < 0){
this.x = x;
this.y = y;   
this.speedX = 2;
this.speedY = 2;
this.speedX = 2;
this.speedY = 2;   
this.gravity = 0;
this.gravitySpeed = 0.5;
this.bounce = 5;
}
if((this.x > 0)&&(this.x<250)&&(this.y>100) && this.speedY > 0){
this.speedY = -this.speedY;

}

    }


    this.hitBottom = function() {
        var rockbottom = myGameArea.canvas.height - this.height;
        if (this.y > rockbottom) {
            this.y = rockbottom;
            this.gravitySpeed = -(this.gravitySpeed * this.bounce);
        }
    }
}
function updateGameArea() {
    myGameArea.clear();
    myGamePiece.newPos();
    myGamePiece.update();
}
</script>


</body>
</html>

He probado diferentes cosas pero o me va...
Os dejo el codigo que he hecho hasta ahora
Código:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
    border:1px solid #d3d3d3;
    background-color: red;
}
</style>
</head>
<body onload="startGame()">
<script>


function startGame() {
    myGamePiece = new component(30, 30, 30, 0, 0);
    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);       
    },
    stop : function() {
        clearInterval(this.interval);
    }, 

    clear function() {
  ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
  ctx.fillRect(0,0,canvas.width,canvas.height);
}
    }
}


function draw() {
  clear();
  ball.draw();
  ball.x += ball.vx;
  ball.y += ball.vy;

function component(width, height, radius, x, y, color) {
    this.type = type;
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;   
    this.speedX = 1;
    this.speedY = 1;
this.vx: 5,
this.vy: 1,
radius: 25,
  color: 'blue',
    this.gravity = 0.1;
    this.gravitySpeed = 0;
    this.bounce = 0.5;
    this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
    this.newPos = function() {
        this.gravitySpeed += this.gravity;
        this.x += this.speedX;
        this.y += this.speedY + this.gravitySpeed;
        this.hitBottom();
if(this.x > 450){
this.speedX = -this.speedX;
}
if(this.x < 0){
this.speedX = -this.speedX;
}
if(this.y < 0){
this.x = x;
this.y = y;   
this.speedX = 2;
this.speedY = 2;
this.speedX = 2;
this.speedY = 2;   
this.gravity = 0;
this.gravitySpeed = 0.5;
this.bounce = 5;
}

    }

    this.hitBottom = function() {
        var rockbottom = myGameArea.canvas.height - this.height;
        if (this.y > rockbottom) {
            this.y = rockbottom;
            this.gravitySpeed = -(this.gravitySpeed * this.bounce);
        }
    }
}

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

</script>

</body>
</html>

Espero que me podaís ayudar


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Convertir una variable a objeto
Programación Visual Basic
abreu20011 2 1,566 Último mensaje 9 Enero 2008, 02:41 am
por abreu20011
convertir objeto en array, para que sería útil?
PHP
jhonatanAsm 7 4,653 Último mensaje 11 Febrero 2012, 06:33 am
por jhonatanAsm
[Ayuda][javascript]undefined elemento de un objeto
Desarrollo Web
LaThortilla (Effort) 1 1,612 Último mensaje 30 Agosto 2016, 20:48 pm
por LaThortilla (Effort)
Método para convertir double a objeto
Programación C/C++
cNoob 1 1,540 Último mensaje 20 Diciembre 2017, 23:26 pm
por srWhiteSkull
[Resuelto] Acceder elemento de objeto javascript « 1 2 »
Desarrollo Web
GMB 10 6,186 Último mensaje 14 Febrero 2020, 22:42 pm
por @XSStringManolo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines