Jump to content

How to display UI 2d text? SOLVED


giorfasolini
 Share

Recommended Posts

Hello again forum, I hope you have a great day :)

I would like to continue my snake project game, and today i have decided to make a scoreboard for each fruits eaten.
However i have an issue,  i cannot display a UI text on my project even though i have followed tutorial in here

The program seems to crash too and wont run.

Any help given is appreciated. Cheers :)

 

Here's my code,  i insert the URL code into createScene function.

<!DOCTYPE html>
<html>
<head>
	<title>Snake Game</title>
	<script src="babylon.js"></script>
	<script src="hand-1.3.7.js"></script>
	<script src="babylonObjLoader.js" ></script>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<canvas id="mycanvas"></canvas>
	<script>
		var canvas = document.getElementById("mycanvas");
		var engine = new BABYLON.Engine(canvas, true);



		var createScene = function() {
			var scene = new BABYLON.Scene(engine);
			var ground = BABYLON.Mesh.CreateGround("ground1", 100, 100, 100, scene);
			//var camera = new BABYLON.FreeCamera("camera2", new BABYLON.Vector3(0,5,-10), scene);
			/*var camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(0, 1, -15), scene);
			scene.activeCamera = camera;scene.activeCamera.attachControl(canvas);
			scene.activeCamera.keysUp.push(87); 
			*/
			var camera = new BABYLON.ArcRotateCamera("camera",0,0,0,BABYLON.Vector3.Zero(0,10,0), scene);
			//var camera = new BABYLON.TouchCamera("camera2", new BABYLON.Vector3(0, 10, 0), scene);
			//var camera = new BABYLON.GamepadCamera("camera2", new BABYLON.Vector3(0, 15, -45), scene);
			//var camera = new BABYLON.DeviceOrientationCamera("camera", new BABYLON.Vector3(0, 1, -15), scene);
			//var camera = new BABYLON.FollowCamera("camera2", new BABYLON.Vector3(0, 15, -45), scene);
    		//camera.lockedTarget = model;
			//camera.setPosition(new BABYLON.Vector3(0,5,-10));

			camera.setTarget(BABYLON.Vector3.Zero(),scene);

			camera.attachControl(canvas,false);

			var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0,1,0), scene);

			light.intesity = 0.5;

			var body = BABYLON.MeshBuilder.CreatePolyhedron("body", {type: 10, size: 0.7},scene);
			var materialBody = new BABYLON.StandardMaterial("bodyMat",scene);
			materialBody.diffuseColor = new BABYLON.Color3(1,0,0);
			body.material = materialBody;
			body.position.y = 5;
			body.position.x = 50;
			body.rotation.x = Math.PI/4;
			body.rotation.z = .19;

				
			/*var head = BABYLON.Mesh.CreateBox("head",0.7,scene);
			var materialHead = new BABYLON.StandardMaterial("HeadMat",scene);
			materialHead.diffuseColor = new BABYLON.Color3(.5,.2,0.3);
			head.material = materialHead;
			head.position.x = -4.5;
			head.position.z = -3;
			head.position.y = 1;
*/
			var canvas2 = new BABYLON.ScreenSpaceCanvas2D(scene, {
        	id: "mycanvas",
        	size: new BABYLON.Size(300, 100),
        	backgroundFill: "#4040408F",
        	backgroundRoundRadius: 50,
        	children: [
            new BABYLON.Text2D("Hello World!", {
                id: "text",
                marginAlignment: "h: center, v:center",
                fontName: "20pt Arial",
            })
        ]
    });

			var apple;
			BABYLON.SceneLoader.ImportMesh("","Finalproject/", "apple.obj", scene, function (meshes) { 
   				apple = meshes[0];
   				//apple.parent = sphere1;
   				  /*engine.runRenderLoop(function() {
			//scene.activeCamera.alpha += .01;
				apple.rotation.y +=0.1;
			scene.render();

				});
*/
   			});

   			var apple2;
			BABYLON.SceneLoader.ImportMesh("","Finalproject/", "apple.obj", scene, function (meshes) { 
   				apple2 = meshes[0];
   				apple2.position.x = 20;
   				apple2.position.z = 20;
		});
   			/*var sphere1 = BABYLON.Mesh.CreateSphere("sphere1",1,1, scene);
			sphere1.position.y = 0.7;
			sphere1.material = myMaterial;
			var myMaterial = new BABYLON.StandardMaterial("myMaterial", scene);
			myMaterial.diffuseColor = new BABYLON.Color3(0, 1, 0);
			*/
			//sphere1.parent = apple;
		


			var snake ;
			BABYLON.SceneLoader.ImportMesh("","Finalproject/", "alphaSnake.babylon", scene, function (newMeshes, particleSystems, skeletons) {

            snake = newMeshes[0];

            snake.position.y = 2;
            snake.rotation.z = -0.07;
            snake.rotation.y = 10;
            snake.rotation.x = 25;
            snake.position = new BABYLON.Vector3(10, 0, 0);  
            //head.parent = snake;
                        
        
            var isvisible = false;
            window.onkeyup = function(event) {
            	event = event || window.event;
				var keyCode = event.which || event.keyCode;

				switch(keyCode) {
					// W key
					case 87:
						snake.rotation.y = Math.PI;
						snake.position.x -=3;
						//scene.beginAnimation(skeletons[0], 0, 60, true, 1.0);
									
						break;

					// A Key
					case 65:
						snake.rotation.y = Math.PI/2;
						snake.position.z -=3;
						//scene.beginAnimation(skeletons[0], 0, 60, true, 1.0);
						
						break;

					// S key
					case 83:
						snake.rotation.y = 25.5;
						snake.position.x +=3;
						//scene.beginAnimation(skeletons[0], 0, 60, true, 1.0);
						
						break;

					// D key
					case 68:
						snake.rotation.y = -Math.PI/2;
						snake.position.z +=3;						
						//scene.beginAnimation(skeletons[0], 0, 60, true, 1.0);
						break;

					/*// E key
						case 69:
							if (snake.intersectsMesh(apple, false)) {
  							isvisible = true;
						}	
						break;
*/
						// J key
						case 74:
							snake.rotation.y = Math.PI/2;
							break;

						// K key
						case 75:
							snake.rotation.y = 25.5;
							break;

						// L key
						case 76:
							snake.rotation.y = -Math.PI/2;
							break;

						// I key
						case 73:
							snake.rotation.y = Math.PI;
							break;

										
				}
			};

			scene.registerBeforeRender(function(){
				apple.rotation.y +=0.1;
				apple2.rotation.y +=0.1;
				if (snake.intersectsMesh(apple, false)) {
  							apple.dispose();
						}

				if (snake.intersectsMesh(apple2, false)) {
  							apple2.dispose();
						}	

				/*if(isvisible == true) {
					apple.dispose();
				}
				*/

					/*sphere1.scaling.x = 5;
					sphere1.scaling.y = 5;
					sphere1.scaling.z = 5;
					sphere1.position.y = 2.5;
					sphere1.material = myMaterial;
					apple.scaling.x = 0;
					apple.scaling.y = 0;
					apple.scaling.y = 0;
					*/
							
            });


        
        });

			
			return scene;
		};

	


		
		var scene = createScene();

		

		engine.runRenderLoop(function() {
			//scene.activeCamera.alpha += .01;
		/*	if(isvisible == true) {
				sphere1.scaling.x = 0;
			}
		*/
			scene.render();

		});

		window.addEventListener("resize",function(){
			engine.resize();
		});

	</script>
</body>
</html>

 

Link to comment
Share on other sites

Thank you for your reply,

I have managed to solve this issue, thank you for your attention @davrous , cheers :)

P.S :  I tried to upload it, however due to my apple and snake are in different files than babylon javascript file, somehow i cant reproduce the snake nor apple image object in playground. Therefore, the program wont run in playground :)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...