Struggling to make the switch operational

I'm struggling to make this switch statement work. I want to have multiple conditions like in an if-else statement, but I can't seem to figure out how to add more than two conditions.

        function spriteAI1() {
        var posX = c2Sprite.position.x
        console.log(c2Sprite.position.x);
        switch(c2Sprite)
        {
        case c2Sprite.position.x > 30: 
        //&& c2Sprite.position.x <= 450:
        c2Sprite.translateX( -7 );
        break;
        //case c2Sprite.position.x < 30: 
        //&& c2Sprite.position.x >= -450:
        //c2Sprite.translateX( 7 );
        //break;
        //case c2Sprite.position.z < 30 
        //&& c2Sprite.position.x < 30:
        //c2Sprite.remove;
        //c2Sprite.clone;
        //break;
        //default: 
        //c2Sprite.translateX( -7 );
        //break;
               }
        }


                            function spriteAI2() {

            if (c2Sprite.position.z >= 30 && c2Sprite.position.z <= 350) { 
            c2Sprite.translateZ( -7 );
            } else if (c2Sprite.position.z <= -30 &&c2Sprite.position.z >= -350) {
            c2Sprite.translateZ( 7 );   
            } 
            //else (c2Sprite.position.z = 30 && c2Sprite.position.x = 30) { 
            //c2Sprite.remove;
            //c2Sprite.clone;
            //}
        }   

Answer №1

case is not designed for comparisons.

When you use case c2Sprite.position.x > 30, you are essentially trying to compare case true or case false, which c2Sprite.position.x is neither.

It would be more appropriate to switch to an if statement, such as:

if (c2Sprite.position.x > 30) { ... }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting attributes on an AngularJS Directive element in real time

My goal is to create a directive in AngularJS with a template that can contain other AngularJS directives. All of my directives require an "id" attribute, so I must set the "id" on the directive within the template. However, no matter how I attempt this, A ...

Experiencing a Typescript issue while trying to set a string as the state of a React component with a specified TS type

I've defined a state in my React component for a specific data type called Color. \\ state const [messageSeverity, setMessageSeverity] = useState<Color>('success'); \\ TS type export type Color = 'success&ap ...

Implementing Do Not Track in an express application

I am trying to implement a feature named "consent" in my Nodejs express app that utilizes the Do Not Track (DNT) functionality from browsers. This function is supposed to integrate Google analytics on rendered pages only when DNT is not active or its state ...

pop-up window that shows chosen choices using javascript or ajax

I have a specific HTML code that allows users to select multiple options. I would like these selected options to be displayed in a popup or a div in real-time as the user makes their selections. I have attempted using a selectbox with checkboxes, but extra ...

Adding input values to a jQuery Ajax POST request

I am currently attempting to send form values to a remote API using AJAX. The necessary information I require from the HTML form element includes the author, string, false, and true. At the moment, I have hard-coded some values but... function sendData ...

Guide to including objects into your project without the need for babel through the use of CDN

I'm struggling with getting my Vue code to transpile properly due to some issues. I have resorted to loading Vue and other packages directly using CDN links, like this: <script src="https://cdnjs.cloudflare.com/ajax/libs/survey-vue/1.8.33/surv ...

The message from the XHR Object appears to be undefined, yet it correctly displays the status

I am currently working on an ajax call that is expected to return a 400 http error code. The backend language being used is PHP. Here is my PHP code: header('Content-Type: text/html',true,400); echo $this->upload->display_errors('< ...

Implementing a JavaScript function with parameters onto an element using backend code

Hey everyone, I've run into a strange issue while trying to pass string parameters to a JavaScript function from the code behind. Here is the current code snippet that I believe is causing the problem: thumbnail = "<a href = 'javascript:Remov ...

Need to discover the most efficient route on a three.js mesh?

I am looking to develop an application with three.js that allows users to paint polygons (specifically, face loops) on a mesh. The idea is for the user to select faces A and B using their mouse in combination with a raycaster. Then, I aim to identify all ...

What is the best way to add a style to the currently active link on a NavLink component using the mui styled() function

I have a custom NavLink component that I want to style with an ".active" class when it is active. However, I am not sure how to achieve this using the "styled()" function in MUI. Does anyone know how to accomplish this? Below is the code for my custom Nav ...

The submission of the form with the ID "myForm" using document.getElementById("myForm").submit() is

<form name="formName" id="formName" action="" method="post" autocomplete="off"> <input type="hidden" name="text1" id="text1" value='0' /> <input type="button" name ="submit" onClick="Submit()" value="submit"> ...

The Angular $http.jsonp() function can only be executed one time

Upon the first response being successful (alert->done), any subsequent hits will result in an 'error' response. I attempted to resolve this issue by adding some config parameters with 'cache: false', but it still only works the firs ...

employing express.json() post raw-body

Within my Express application, I am aiming to validate the length of the request body and restrict it irrespective of the content type. Additionally, I wish to parse the body only if the content type is JSON. How can I go about accomplishing this? Curren ...

How to Embed a Javascript (jquery) Variable within a JSON Object

I have searched everywhere for a "simple answer" to this problem, but unfortunately, I cannot find a solution that aligns with my understanding of JSON and jQuery. Perhaps I am too much of a beginner in JSON to accurately formulate the right question (and ...

Executing a component's method using HTML in Vue2

In my development project, there is a .vue file named ServiceList. This file imports the component called Information.vue. My objective is to execute the code from the Information component in a loop within the template of the ServiceList file. Here is an ...

Error: The function "execute" has not been declared

Hey there! I've created a Discord bot that is meant to check the status of a Minecraft server, but I'm encountering an issue with the embed. It's showing this error: UnhandledPromiseRejectionWarning: ReferenceError: execute is not defined. ...

The error message "Cannot access property 'findAll' of undefined in expressjs" is displayed

An issue has occurred with ExpressJS: TypeError - Cannot read property 'findAll' of undefined. It seems that all Sequelize functions are not working correctly. Numerous errors are popping up, such as "Cannot read property 'sequelize method& ...

Recursive rendering of tree components in React

I am facing a challenge in rendering tree items recursively using React. I have been struggling to achieve the desired outcome as calling treeRender(children) seems to alter the data structure when a folder is encountered for the first time. I am curious a ...

A loop that iterates through only the initial element of an array

When trying to call in the array values using the code snippet below, I encountered an issue. Upon clicking, I am able to successfully retrieve the first material in the array, but then it stops looping through the rest of the materials. Can someone plea ...

Tips for executing Cross-Origin-Requests from Firebase Hosting to an AWS Lambda function

Excuse me, I'm still new to web development. I have a basic single-page website hosted on Firebase hosting that retrieves some information from an AWS Lambda function (I didn't use Google Cloud because it didn't support outbound requests fo ...