2009-06-12 14 views
1

Dies ist der aktuelle Zustand meines Codes, und obwohl ich den gewünschten Effekt erziele, funktioniert es nicht so, wie ich es brauche. Da sich das Programm in einer Endlosschleife befindet, erzeugt es natürlich beide Hintergrundgradienten konstant und 10 Kreise mit jeder Schleife, und schnell überproduzieren sie sich selbst und verlangsamen das Programm ganz nach unten.Wie kann ich diesen Shoes-Code vom Herunterfahren des Computers machen?

geht hier:

Shoes.app (:title => 'Circles', :width => 500, :height => 500, :resizable => false) do 
      i = 0 
# Animation loop 
    animate (24) do |i| 
# Variables For Randomized Colours 
      randomCol = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
      randomCol2 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
      randomCol3 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
      randomCol4 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
      background randomCol..randomCol2 
      fill randomCol3 
      stroke randomCol4 
      strokewidth  (0..5).rand 
# Generate 10 circles per loop cycle 
      10.times{ 
      i += 1 
      oval :left => (-5..self.width).rand, 
      :top => (-5..self.height).rand, 
      :radius => (1..100).rand 
      } end 
     end 

ich versucht habe, was ich denken kann, aber ich bin nicht allzu vertraut mit Rubys Syntax, oder was kann ich oder kann nicht mit Schuhen tun, die ich tun kann, mit Rubin. Einige Ratschläge, wohin Sie von hier aus gehen, würden sehr geschätzt werden.

+1

Wenn Sie die Erzeugung der Kreise steuern mögen, eine Variable hinzu zu zählen, wie Viele wurden produziert und hören dann auf, sie zu produzieren, wenn zu viele erzeugt wurden. –

Antwort

5

Jeder dieser Ovale und Hintergründe, die Sie zeichnen, ist ein separater Gegenstand im Speicher, was bedeutet, dass er nach einer Weile abstürzt. Wenn Sie nur das letzte Bild zeigen, wollen Sie gezeichnet, dann müssen Sie die App jedes Mal löschen:

Shoes.app (:title => 'Circles', :width => 500, :height => 500, :resizable => false) do 

    # Animation loop 
    animate (24) do |i| 
    app.clear 

    # Variables For Randomized Colours 
    randomCol = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol2 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol3 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol4 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 

    background randomCol..randomCol2 
    fill randomCol3 
    stroke randomCol4 
    strokewidth (0..5).rand 

    # Generate 10 circles per loop cycle 
    10.times do |i| 
     i += 1 
     oval :left => (-5..self.width).rand, 
     :top => (-5..self.height).rand, 
     :radius => (1..100).rand 
    end 
    end 
end 

, die als Ihr Original (außer der Tatsache, dass sie auf unbestimmte Zeit laufen werden) nicht so cool ist, weil Sie den Schichtungseffekt nicht mehr haben. In diesem Fall können wir es einige Male durchlaufen lassen, bevor wir es löschen.

Shoes.app (:title => 'Circles', :width => 500, :height => 500, :resizable => false) do 

    # Animation loop 
    animate (24) do |i| 
    app.clear if (i % 6 == 0) 

    # Variables For Randomized Colours 
    randomCol = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol2 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol3 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol4 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 

    background randomCol..randomCol2 
    fill randomCol3 
    stroke randomCol4 
    strokewidth (0..5).rand 

    # Generate 10 circles per loop cycle 
    10.times do |i| 
     i += 1 
     oval :left => (-5..self.width).rand, 
     :top => (-5..self.height).rand, 
     :radius => (1..100).rand 
    end 
    end 
end 

nun eine noch interessante Strategie der letzten n Pässe zu halten wäre und die älteste zu räumen, so dass wir immer haben, sagen sie, 6 Schichten: In diesem Beispiel wird es jedes sechstes Mal durch löschen auf dem Bildschirm (können variieren finde ich 6 ein guter Cutoff-Punkt zu sein, aber Ihre Meinung (und Leistung des Computers)!):

Shoes.app (:title => 'Circles', :width => 500, :height => 500, :resizable => false) do 
    n = 6 
    @layers = [] 
    n.times { @layers << [] } 
    # Animation loop 
    animate (24) do |i| 
    oldest = i % n 
    # Clear out oldest frame 
    @layers[oldest].each {|x| x.remove} 
    @layers[oldest] = [] 

    # Variables For Randomized Colours 
    randomCol = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol2 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol3 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 
    randomCol4 = rgb((0..255).rand, (0..255).rand, (0..255).rand, (0..255).rand) 

    @layers[oldest] << background(randomCol..randomCol2) 
    fill randomCol3 
    stroke randomCol4 
    strokewidth (0..5).rand 

    # Generate 10 circles per loop cycle 
    10.times do |i| 
     @layers[oldest] << oval (:left => (-5..self.width).rand, 
     :top => (-5..self.height).rand, 
     :radius => (1..100).rand) 
     end 
    end 
end 
+0

Cheers für die Hilfe Mann - ich habe beschlossen, "3" als Cutoff-Punkt, als, kann mein System wirklich nicht viel mehr = P Vielen Dank dafür - Ich habe viel mehr über Rubys Syntax gelernt, als so wie die Zählmethode, die ich arbeiten könnte, aber nie herausfinden könnte, wie ich sie so bedienen soll, wie ich es wollte! lol. Ich schulde dir eine =) Grüße, G0DL355 –

Verwandte Themen