Triggering an excessive number of events upon attaching a listener to my sprites

I'm experimenting with creating a simple Cocos2d-js demo featuring clickable balls that can be moved. Here is how I am generating the balls:

        var listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: false,
            onTouchBegan: ballTouchBeganEventHandler
        });

        var BallTile = cc.Sprite.extend({
            ctor: function(image, position)
            {
                this._super();
                var ballSize = g_settings.ballSize;
                this.initWithFile(image, cc.rect(0, 0, ballSize, ballSize));
                this.setPosition(position);
                cc.eventManager.addListener(listener.clone(), this);
            }
        });

        var ball1 = new BallTile(ballImage1, ballPosition1);
        var ball2 = new BallTile(ballImage2, ballPosition2);
        var ball3 = new BallTile(ballImage3, ballPosition3);

The issue I'm facing is that when I click on any of the balls, the event gets triggered three times (once for each ball) instead of just once for the specific ball clicked. Even though all the balls share the same event listener, my expectation was that it would only trigger for the individual ball clicked, not for every ball associated with the listener.

What could be causing this behavior?

Answer №1

Experiment with changing the value of swallowTouches to true, and confirm returning true within the ballTouchBeganEventHandler.

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

Calling Functions in JavaScript Through Events: A Beginner's Guide

As I dive into learning JavaScript, one thing that stumps me is figuring out how to call a function from an event. Currently, the only method I am familiar with involves using anonymous functions (as seen in my code example below), but I'm curious if ...

Instead of using colons, display the separation of hours, minutes, and seconds with underscores

Currently, I am utilizing moment.js to retrieve the present date and time with the intention of formatting it in the specific format below 'MMMM Do YYYY, h:mm:ss a' Unfortunately, the problem arises as the delineation between hours, minutes, and ...

Troubleshooting Loading Problems in React Native Using Geny Motion

Currently utilizing react native Using react-native-cli version 2.0.1 and react-native version 0.55.3 Execution was done through react-native run-android Encountering a perpetual loading screen while running the app in debugger mode, with 127.0.0.1:8081 ...

transfer the value of a method to a different component

In my Component called IncomeList, there is a method named sumValue. This method simply adds different numbers together to produce one value, for example 3+5 = 8. Similarly, in another Component named OutputList, the same logic is applied but with a method ...

Having difficulty retrieving data from JSON file using Backbone framework

When I click on the div, I am attempting to retrieve JSON data but am unable to view the output. I am utilizing Backbone Collection to fetch JSON data. I have tried displaying the JSON data in the console as well as within another div. The contents of the ...

Tips on capturing a URL using JQuery

Currently, I am in the process of importing an external HTML file into my webpage. Within this file, there is a JavaScript method linked to a form submission event. function ValidateInput(){ //some code if(validationFailed){ ...

Why are these additional curly brackets added within an else statement?

Upon reviewing some typescript code, I noticed the presence of extra curly braces within an else block. Are these additional braces serving a specific purpose or are they simply used to provide further grouping for two nearly identical code snippets? Consi ...

Enhanced MUI TextField component with active cursor and focused state

When using the MUI TextField component as a single input form, I encountered an issue where the component loads with focus but no visible cursor to begin typing. To start typing, the user must click into the input field or press the tab key, which then act ...

Using jQuery to display items from GitHub API in a custom unordered list format

Attempting to access data from the GitHub API using jQuery (AJAX) and display it on a static webpage. Here are the HTML and JS code snippets: $(document).ready(function(){ $.ajax({ url: 'https://api.github.com/re ...

Is there a way for me to determine the specific link that I have clicked on

I am implementing a table where users can edit the columns. Each cell contains an <a> tag that triggers a modal to change the value in the database and refresh the page. The issue I'm facing is that once the modal appears, the code doesn't ...

Storing TypeScript functions as object properties within Angular 6

I am working on creating a simplified abstraction using Google charts. I have implemented a chartservice that will act as the abstraction layer, providing options and data-source while handling the rest (data retrieved from a REST API). Below is the exist ...

Using Angular 4 constructor value in a condition with *ngIf

Within this TypeScript snippet, there is a variable called moreinfo that is initially set to 1. In the constructor, however, the value of moreinfo is changed to 2. Subsequently, based on whether the value is 1 or 2, different div elements are displayed usi ...

"Using jQuery to ensure multiple sections are ready for manipulation on

After some experimentation, I made an interesting discovery: I can actually include $(document).ready(function(){}); multiple times. For example: $(document).ready(function(){ var abc = "1122"; //do something.. }); $(document).ready(function() ...

Guide on configuring and executing AngularJS Protractor tests using Jenkins

I am encountering an error with the following configuration: ERROR registration capabilities Capabilities [{platform=WINDOWS, ensureCleanSession=true, browserName=internet explorer, version=}] does not match the current platform LINUX 18:17:05.892 INFO ...

Generating requests using ExpressJS

Is it possible to send a POST request using a GET action? Although everything seems to be working fine, the "TOKEN" does not appear after the post. I am puzzled as to why this is happening. const request = require('request'); exports.g ...

How to eliminate unnecessary JSON data using JavaScript

Recently delving into the realms of JSON and JavaScript, I found myself tasked with creating a table in Cloudant NoSQL. Upon gathering Weather data from a reliable Weather Company in JSON format to upload onto Cloudant, I encountered some irrelevant data t ...

Scrolling back to the top of the page using Jquery instead of a specific div

Check out the code for my project here. The isotope feature is functioning correctly, however, when clicking on the image, instead of scrolling to the navigation under the red box as intended, the page scrolls all the way to the top. If I modify the follo ...

The PHP shopping cart file is returning an undefined JSON response

Currently, I am developing a shopping cart system using a combination of PHP and JavaScript with an xmlhttprequest call to handle the AJAX functionality. The issue I am facing is that whenever the user clicks on the 'add to cart' button, the prod ...

encountered errors loading static assets in express.js

Just getting started with node development. var app = require('express')() , server = require('http').createServer(app) , io = require('socket.io').listen(server); Here is my current content: server.listen(1337); app.g ...

Simple steps to correct the npm installation of the React list filter

I encountered an issue while trying to install react-list-filter using npm (npm install react-list-filter). The error messages displayed in my console are as follows: npm ERR! code ETARGET npm ERR! notarget No matching version found for <a href="/cdn-c ...