IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Undefined ? Comment le résoudre ?


Sujet :

JavaScript

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2018
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2018
    Messages : 1
    Par défaut Undefined ? Comment le résoudre ?
    Bonjour à tous, je suis en train de développer un jeu pour mon projet tuteuré, mon problème est que je défini des variables globales, je les modifies dans une des fonctions et après quand j’essaie d'accéder depuis une autre fonction je ne peut pas car ça me dit que la variable est indéfinie. Si quelqu'un pourrai m'aider ça serrait génial, merci d'avance.

    Voici le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
     
    $(document).ready(function(){
    //Declarations
    var canvas, context, width, height, frames = 0, floor, floor_position, actual_status = "play", character, character_position, game_velocity = 6, jump_force = 20,
    maxJump = 1, background, players;
    var actionLeft = actionRight = actionJump = action = actionHold = actionBomb = actionEsc = startAction = false;
     
     
    //Class floor
    class Floor{
    	constructor(position_floor, width_floor, height_floor){
    		this.position = position_floor;
    		this.height = height_floor;
    		this.width = width_floor;
    		this.color = "#ffdf70";
    	}
     
    	//Drawing the floor
    	draw_floor(){
    		context.fillStyle = this.color;
    		context.fillRect(this.position.x, this.position.y, this.width, this.height);
    	}
     
    }
     
    //Class position
    class Position{
    	constructor(x_const, y_const){
    		this.x = x_const;
    		this.y = y_const;
    	}
     
    		right(){
    			if(this.x < width-50 ){
    				this.x += game_velocity;
    			} else if (this.x >= width-50 ){
    				this.x = width-50;
    			}
     
    		}
     
    		left(){
    			if(this.x > 0 ){
    				this.x -= game_velocity;
    			} else if (this.x <= 0 ){
    				this.x = 0;
    			}
    		}
     
    		jump(object){
    			if(object.number_of_jump < maxJump){
    				object.velocity = -jump_force;
    			}
    		}
     
    	}
     
    //Class Player
    class Player{
    	constructor(id, name, color){
    		this.id = id
    		this.name = name;
    		this.velocity = 0;
    		this.width = 50;
    		this.height = 50;
    		this.gravity = 1;
    		this.color = color;
    		this.position = new Position(Math.floor(width * Math.random()), Math.floor(height * Math.random()));
    		this.number_of_jump = 0;
    	}
     
     
    	draw_character(){
    		// var character_icon = new Image();
    		// character_icon.src = "./images/character.png";
    		// context.drawImage(character_icon, -this.width/2, -this.height/2);
     
    		context.fillStyle = this.color;
    		context.fillRect(this.position.x, this.position.y, this.width, this.height);
    	};
     
    	refresh_character(){
    		this.velocity += this.gravity;
    		this.position.y += this.velocity;
     
     
    		if (this.position.y > (floor.position.y - this.height) && actual_status != "lost"){
    			this.position.y = floor.position.y - this.height;
    			this.velocity = 0;
    			this.number_of_jump = 0;
    		}
     
    	};
     
    }
     
     
     
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////Starting the Script//////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    function main(){
    	actual_status = "play";
     
    	//Getting window information(height and width)
    			height = window.innerHeight;
    			width = window.innerWidth;
     
    	//Creating the canvas
    	canvas = document.createElement("canvas");
    	canvas.height = height;
    	canvas.width = width;
     
    	canvas.style.border = "1px solid red";
     
    	context = canvas.getContext("2d");
     
    	//Adding canvas to html
    	document.body.appendChild(canvas);
     
    	menu();
     
    	floor_position = new Position(0, 550);
    	floor = new Floor(floor_position, width, 50);
     
    }
     
    function menu(){
    	console.log(floor);
    		drawSun();
     
    		start();
    }
     
     
    main();
     
     
     
    function refresh(){
    }
     
    //Function play is the function wich permit to the game to have an infinite loop
    function start(){
    	//Permit to request for commands
    //	sendAjax();
     
    	refresh();
    	draw();
     
    	window.requestAnimationFrame(start);
    }
     
    function draw(){
         floor.draw_floor(); //LE BUG EST ICI QUAND J'essaie d'accéder à la fonction draw_floor de mon objet floor ça dit que flor est indéfini
    }

  2. #2
    Invité
    Invité(e)
    Par défaut
    bj,

    ne peux tu tout simplement pas mettre des console.log et suivre ta stack trace
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    (floor is undefined)
    calls::main 
        (floor is undefined)
        calls::menu
            (floor is undefined)
            calls::start
                (floor is undefined)
                calls::draw
                    (floor is undefined)
        floor<=new ..
    a noter: comme tu l'as constaté, le pb c'est toute tes variables globales...

    indépendamment de tes variables globales ou classe, généralement t'as envie d'avoir une fonction init qui initialise tes variables et que t'appeles avant tt le reste. SI t'as une variable qui doit être initialisée plus tard, bon, faut réfléchir plus.

  3. #3
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Billets dans le blog
    125
    Par défaut


    Avec un peu d'ordre et de méthode, on peut écrire le fichier JS comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    //Declarations
    let 
        canvas, 
        context, 
        width, 
        height, 
        frames = 0,
        floor, 
        floor_position, 
        actual_status = "play",
        character, 
        character_position, 
        game_velocity = 6,
        jump_force = 20,
        maxJump = 1,
        background, 
        players,
        actionLeft = false,
        actionRight = false,
        actionJump = false,
        action = false,
        actionHold = false,
        actionBomb = false,
        actionEsc = false,
        startAction = false;
     
    //Class floor
    class Floor {
        constructor(position_floor, width_floor, height_floor) {
            this.position = position_floor;
            this.height = height_floor;
            this.width = width_floor;
            this.color = "#ffdf70";
        }
     
        //Drawing the floor
        draw_floor() {
            context.fillStyle = this.color;
            context.fillRect(this.position.x, this.position.y, this.width, this.height);
        }
    }
     
    //Class position
    class Position {
        constructor(x_const, y_const) {
            this.x = x_const;
            this.y = y_const;
        }
     
        right() {
            if (this.x < width - 50) {
                this.x += game_velocity;
            } else if (this.x >= width - 50) {
                this.x = width - 50;
            }
        }
     
        left() {
            if (this.x > 0) {
                this.x -= game_velocity;
            } else if (this.x <= 0) {
                this.x = 0;
            }
        }
     
        jump(object) {
            if (object.number_of_jump < maxJump) {
                object.velocity = -jump_force;
            }
        }
    }
     
    //Class Player
    class Player {
        constructor(id, name, color) {
            this.id = id
            this.name = name;
            this.velocity = 0;
            this.width = 50;
            this.height = 50;
            this.gravity = 1;
            this.color = color;
            this.position = new Position(Math.floor(width * Math.random()), Math.floor(height * Math.random()));
            this.number_of_jump = 0;
        }
     
     
        draw_character() {
            // let character_icon = new Image();
            // character_icon.src = "./images/character.png";
            // context.drawImage(character_icon, -this.width/2, -this.height/2);
     
            context.fillStyle = this.color;
            context.fillRect(this.position.x, this.position.y, this.width, this.height);
        };
     
        refresh_character() {
            this.velocity += this.gravity;
            this.position.y += this.velocity;
     
     
            if (this.position.y > (floor.position.y - this.height) && actual_status != "lost") {
                this.position.y = floor.position.y - this.height;
                this.velocity = 0;
                this.number_of_jump = 0;
            }
     
        };
     
    }
     
    function draw() {
        floor.draw_floor(); //LE BUG EST ICI QUAND J'essaie d'accéder à la fonction draw_floor de mon objet floor ça dit que flor est indéfini
    }
     
    function menu() {
        console.log(floor);
     
        // drawSun(); // indéfini
        start();
    }
     
    function refresh() {
        console.log(`fonction inexistante`);
    }
     
    //Function play is the function wich permit to the game to have an infinite loop
    function start() {
        //Permit to request for commands
        //	sendAjax();
     
        refresh();
        draw();
     
        window.requestAnimationFrame(start);
    }
     
    // Starting the Script
    function main() {
        actual_status = "play";
     
        //Getting window information(height and width)
        height = window.innerHeight;
        width = window.innerWidth;
     
        //Creating the canvas
        canvas = document.createElement("canvas");
        canvas.height = height;
        canvas.width = width;
     
        canvas.style.border = "1px solid red";
     
        context = canvas.getContext("2d");
     
        //Adding canvas to html
        document.body.appendChild(canvas);
     
        floor_position = new Position(0, 550);
        floor = new Floor(floor_position, width, 50);
     
        menu();
    }
     
    document.addEventListener('DOMContentLoaded', ev => {
        // le DOM est construit, la page web n'est pas visible
     
        // début code du test
     
     
        // fin code du test
     
    }, {
        capture: false,
        passive: true,
        once: false
    });
     
    window.addEventListener('load', ev => {
        // le DOM est construit et la page web est visible
     
        // début code du test
     
        main();
     
        // fin code du test
     
    }, {
        capture: false,
        passive: true,
        once: false
    });
    Lors du test, la méthode drawSun est inexistante et le code achoppe sur la fonction indéfinie refresh.

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

Discussions similaires

  1. Réponses: 5
    Dernier message: 23/06/2008, 21h08
  2. [MySQL] Erreur MySQL, Comment la résoudre ?
    Par lusos dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 28/04/2008, 14h26
  3. SQL*Loader-510 Error - Comment le résoudre?
    Par lem01 dans le forum SQL
    Réponses: 7
    Dernier message: 22/02/2008, 16h56
  4. Réponses: 5
    Dernier message: 08/11/2006, 15h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo