2

i´m working on an interactive Infographic. Basically there are is a number of circles flying around. I try to change the amount of circles by clicking on a button, therefor I want to increase the variable d by 1 on click, to get an other element from the array. But it doesnt´t work. Any ideas?

var leuko = ["167", "143", "134", "96"]; //array


var d = 0; //variable

$("#day").click(function() {

d += 1;

_

for (var i = 0; i < leuko[d]; i++) {

var x = 20 + (Math.random() * (canvasWidth - 40));
var y = 20 + (Math.random() * (canvasHeight - 40));

var radius = 5;
var vX = Math.random() * 0.2;
var vY = Math.random() * 0.2;

I hope you can understand my problem, as my english isn´t the best.

Here is a jsfiddle-link. Layout looks a little bit messed up..

http://jsfiddle.net/JDU6H/

Thanks!

8
  • 1
    your script is incrementing d, but the problem is, that this variable is used only once on document ready.. Commented Nov 10, 2012 at 15:19
  • Yes, d is incrementing fine. jsfiddle.net/JDU6H/1 Commented Nov 10, 2012 at 15:20
  • 1
    If you want to totally rewrite your code, I could help you in StackOverflow chat here. My first language is German ;) Commented Nov 10, 2012 at 15:24
  • Sounds good to me ;) Which Channel? Commented Nov 10, 2012 at 15:34
  • @FelixSchneider Click here: chat.stackoverflow.com/rooms/19373/… Commented Nov 10, 2012 at 15:48

1 Answer 1

1

Without rewriting many parts of your code, you can simply change your click function:

$("#day").click(function() {
    d += 1;

    var x = 20 + (Math.random() * (canvasWidth - 40));
    var y = 20 + (Math.random() * (canvasHeight - 40));

    var radius = 5;
    var vX = Math.random() * 0.2;
    var vY = Math.random() * 0.2;

    zellen.push(new Zelle(x, y, radius, vX, vY));
});

http://jsfiddle.net/JDU6H/2/

Nevertheless I'd recommend you creating a class for all cells and adding an add() function.

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

1 Comment

This works fine. The only problem is, that I dont want to add just one more circel, but change the elemet from the array. For example I want to draw leuko[0] (167 circles) and then change the element to leuko[1] (143 circles) and so on...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.