1

I asked another version of this question on the gamedev.SE site earlier today but figured I could get better answers here. Also reformulated the question some. I hope this is okay couldn't find anything prohibiting it in the FAQ

I'm playing around with making a puzzle game, haven't done that much before I run into my first problem.

Question a) Basically, I want to create a certain amount of the same object/function (a ball). But the objects will be created dynamically (since the amount of balls could range from 3 to 25), preferably with a for loop pushing the different balls to an array.

However I'm not experienced enough to figure out a good way to do this.

Also, once I have my array of balls on the canvas, I want to be able to select one of the balls.

Question b) How do I know/determine which ball in the array was clicked? Is a simple for loop enough and accessing the objects with [i]?

I made a jsFiddle example of what I want with 1 ball (you need to click the orange ball to select, then you can move it around by clicking anywhere on the canvas).

This is what I want to do, but with more balls, and the amount of balls is not specified (deciding the amount part i got covered). How could I solve this in a easy way?

Help appreciated.

2 Answers 2

2

Using a for loop is fine for your object generation.

As for detecting when you click an object in the canvas, this tutorial might help. It appears there is no built-in feature for tracking objects in your canvas - you simply do manual hit detection based on the common pixels under your mouse and object

Hope that helps

Sign up to request clarification or add additional context in comments.

Comments

0

Paper.js has great DOM model, and out of the box ability to click and select items

http://paperjs.org/tutorials/project-items/transforming-items/

hit testing:

http://paperjs.org/examples/hit-testing/

3 Comments

I don't want to use external libraries, that doesn't teach me anything. And the clicking part I got covered. I need to know how to do this on a bigger scale.
a library is not a black box , you can still read the source code.
Yes but most of the time it has a couple of 1000 lines of code. That makes it difficult to get the pieces you need without putting down a month of your life. Reverse engineering is not my strong side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.