Hey, again I have a question!
My code isn’t working how I’d like it to be because it is kind of “buggy” and I don’t know where the problem is tbh in the console is a a error:
three.js:10505 THREE.Object3D.add: object not an instance of THREE.Object3D. undefined
so I can’t really find the problem, can you help me?
<body></body>
<script src="/three.js"></script>
<script>
// The "scene" is where stuff in our game will happen:
var scene = new THREE.Scene();
var flat = {flatShading: true};
var light = new THREE.AmbientLight('white', 0.8);
scene.add(light);
// The "camera" is what sees the stuff:
var aspectRatio = window.innerWidth / window.innerHeight;
var camera = new THREE.PerspectiveCamera(75, aspectRatio, 1, 10000);
camera.position.z = 500;
// The "renderer" draws what the camera sees onto the screen:
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// BODY BODY BODY BODY BODY BODY BODY BODY BODY
var marker = new THREE.Object3D();
scene.add(marker);
var body = new THREE.SphereGeometry(100, 25, 25);
var cover = new THREE. MeshNormalMaterial();
var avatar = new THREE.Mesh(body, cover);
marker.add(avatar);
// HAND HAND HAND HAND HAND HAND HAND HAND HAND HAND
var hand = new THREE.SphereGeometry(50, 25, 25);
var rightHand = new THREE.Mesh(hand, cover);
rightHand.position.set(-150, 0, 0);
avatar.add(rightHand);
var leftHand = new THREE.Mesh(hand, cover);
leftHand.position.set(150, 0, 0);
avatar.add(leftHand);
var foot = new THREE.SphereGeometry(50, 25, 25);
// FOOD FOOD FOOD FOOD FOOD FOOD FOOD
var rightFood = new THREE.Mesh(foot, cover);
rightFood.position.set(-70, -130, 0);
avatar.add(rightFood);
var leftFood = new THREE.Mesh(foot, cover);
leftFood.position.set (70, -130, 0);
avatar.add(leftFood);
//EYES EYES EYES EYES EYES EYES
var Eyes = new THREE.SphereGeometry(10);
var rightEye = new THREE.Mesh(Eyes, cover);
rightEye.position.set(-33, 10, 90);
avatar.add(rightEye);
var leftEye = new THREE.Mesh(Eyes, cover);
leftEye.position.set(33, 10, 90);
avatar.add(leftEye);
// DETAILS
var Mouth = new THREE.TorusGeometry(10,2, 100, 25, 3.14);
var cover = new THREE.MeshNormalMaterial(flat);
var Mund = new THREE.Mesh(Mouth, cover);
Mund.position.set(0, 0, 100);
Mund.rotation.set(0, 0, 3.14);
avatar.add(Mund);
// Zylinder Zylinder Zylinder Zylinder Zylinder Zylinder
var Zylinder = new THREE.CylinderGeometry(80, 70, 110, 50);
var cover = new THREE.MeshNormalMaterial(flat);
var Hut = new THREE.Mesh(Zylinder, cover);
avatar.add(Hut);
Hut.position.set(0, 120, 0);
var SeiteZylinder = new THREE.CylinderGeometry(100, 100, 10, 50);
var cover = new THREE.MeshNormalMaterial(flat);
var Hut2 = new THREE.Mesh(SeiteZylinder, cover);
avatar.add(Hut2);
Hut2.position.set(0, 75, 0);
// STAB STAB STAB STAB STAB STAB STAB STAB STAB
var Stab = new THREE.CylinderGeometry(10, 10, 240, 25);
var cover = new THREE.MeshNormalMaterial(flat);
var Stock = new THREE.Mesh(Stab, cover);
avatar.add(); // .add(Stock); um STock hinzuzufügen!
Stock.position.set(150, -50, 0);
marker.add(camera);
// BÄUME
function makeTreeAt(x, z) {
var trunk = new THREE.Mesh(
new THREE.CylinderGeometry(50, 50, 200),
new THREE.MeshBasicMaterial({color: 'sienna'})
);
var top = new THREE.Mesh(
new THREE.SphereGeometry(150),
new THREE.MeshBasicMaterial({color: 'forestgreen'})
);
top.position.y = 175;
trunk.add(top);
trunk.position.set(x, -75, z);
scene.add(trunk);
}
makeTreeAt(500, 0);
makeTreeAt(-500, 0);
makeTreeAt(750, -1000);
makeTreeAt(-750, -1000);
// ANIMATION ANIMATION ANIMATION ANIMATION ANIMATION
var clock = new THREE.Clock();
var isCartwheeling = false;
var isFlipping = false;
var isMovingRight = false;
var isMovingLeft = false;
var isMovingForward = false;
var isMovingBack = false;
function animate() {
requestAnimationFrame(animate);
walk();
acrobatics();
renderer.render(scene, camera);
}
animate();
function walk() {
if (!isWalking()) return;
var speed = 10;
var size = 100;
var time = clock.getElapsedTime();
var position = Math.sin(speed * time) * size;
rightHand.position.z = position;
leftHand.position.z = -position;
leftFood.position.z = position;
rightFood.position.z = -position;
}
function isWalking() {
if (isMovingRight) return true;
if (isMovingLeft) return true;
if (isMovingForward) return true;
if (isMovingBack) return true;
return false;
}
function acrobatics() {
if (isCartwheeling) {
avatar.rotation.z = avatar.rotation.z + 0.05;
}
if (isFlipping) {
avatar.rotation.x = avatar.rotation.x + 0.05;
}
}
document.addEventListener('keydown', sendKeyDown);
function sendKeyDown(event) {
var code = event.code;
if (code == 'KeyA') {
marker.position.x = marker.position.x - 5;
isMovingLeft = true;
}
if (code == 'KeyD') {
marker.position.x = marker.position.x + 5;
isMovingLeft = true;
}
if (code == 'KeyW') {
marker.position.z = marker.position.z + 5;
isMovingLeft = true;
}
if (code == 'KeyS') {
marker.position.z = marker.position.z - 5;
isMovingLeft = true;
}
if (code == 'KeyC') isCartwheeling = !isCartwheeling;
if (code == 'KeyF') isFlipping = !isFlipping;
}
document.addEventListener('keyup', sendKeyUp);
function sendKeyUp(event) {
var code = event.code;
if (code == 'KeyA') isMovingLeft = false;
if (code == 'KeyD') isMovingRight = false;
if (code == 'KeyS') isMovingBack = false;
if (code == 'KeyW ') isMovingForward = false;
}
// Now, show what the camera sees on the screen:
renderer.render(scene, camera);
</script>
Okay ty for help and have a nice day!