How to change to same image every 4 times(unity3D) - loops

I've got a 2d image. What I want to make it do is- everytime I click the sprite it would change an image(in a specific order) and repeat the image every 4 times I click it(So it loops the order every 4 times). I don't have an idea for how to do it because I'm new to unity. Thanks in advance.

Presumably you've got an array or list of the images in the order you want them shown in (I'll call this images), and you've got a way of detecting a click on the sprite. There are many ways you can do what you want in code, one of them would look something like this:
int imageNum = 0; //this should be class wide
...
<sprite click detection>
{
imageNum++; //increment the imageNum
imageNum = imageNum >= 4 ? 0 : imageNum; //reset to 0 on the 4th click
sprite = images[imageNum]; //set the sprite to the next image
}
If you post what you've already tried I can make this code closer to what you actually want.

Try learning some c# or basic java tutorials before jumping into the world of unity, it will help tons. As for the question, create a script for the object or clicking an event with an basic counter that increments the value by 1 for every click. something like:
while(counter = 4)
{
picture.source = <new picture>
counter.int = 1
}
or something to that effect.

Related

Movie Clip Object adding Multiple Times with Enter_Frame Event

Hey Guys so I am using TweenLite to act as a timer for Performance with Mobile usage. I ran into a little problem here that I can't seem to figure out. So In my ENTER_FRAME Listener I have this function difficultyController Where I add my TweenLite control like so:
private function difficultyController():void
{
if (nScore >= 10)
{
TweenLite.delayedCall(nChainsaw, addChainsaw);
trace("DIFFICULTY_UPDATE");
}
}
In my addChainsaw Function I have the Movie Clip Objects added to the stage like so:
private function addChainsaw():void
{
TweenLite.delayedCall(nChainsaw, addChainsaw);
var newChainsaw = new mcChainsaw();
//Add Child
addChild(newChainsaw);
//Push Move CLips into array
aChainsawArray.push(newChainsaw);
trace(aChainsawArray.length + "chainsaw");
}
Now I want the chainsaw Movie clip in the array to be added to the stage every 2 seconds which is what the value of nChainsaw is. Ill kill it off when the nScore reaches a higher number. But as of right now when I test the Game like so It adds Multiple Movie clips over and over again then freezes the game. I know it has to do with the ENTER_FRAME Listener But I don't know what else to do. SHould I just remove the TweenLite and add A Actual Timer and in the diffucultyController(); just add timer.start??
If anyone has anyother Ideas I would really Appreciate it thanks!

Movie Clip through Display Object Not working correctly

Hey everyone so I have a movie Clip called popEffect that i want to show on the current bubbles that are being clicked by the mouse. Now Whenever I click on a Bubble everything works correctly they get removed from the stage, but the problem I am having is that the popEffect is not positioned to the current bubbles that are being clicked. Instead they are positioned at a different bubble that shows on the screen in the display object array.
Here is how I have it all set up:
private function addBubbles(e:TimerEvent):void
{
bubbles = new mcBubbles();
stage.addChild(bubbles);
aBubbleArray.push(bubbles);
bubbles.addEventListener(MouseEvent.CLICK, bubblesBeingClicked);
}
Then the BubblesBeingClicked function:
private function bubblesBeingClicked(e:MouseEvent):void
{
var BubblePop:DisplayObject = e.target as DisplayObject; // HERE is your clicked square
var i:int = aBubbleArray.indexOf(BubblePop); // and HERE is your index in the array
if (i < 0)
{
// the MC is out of the array
//trace("Pop Clicked");
onBubbleIsClicked(BubblePop);
aBubbleArray.splice(i, 1);
BubblePop.parent.removeChild(BubblePop);
//Remove Listeners!!!
BubblePop.removeEventListener(MouseEvent.MOUSE_DOWN, onBubbleIsClicked);
// Null
BubblePop = null;
}
}
Finally my onBubbleIsClicked function where i have the popEffect located:
private function onBubbleIsClicked(bubblePop:DisplayObject):void
{
nScore++;
updateHighScore();
//Pop Effect
popEffect = new mcBubblePop();
stage.addChild(popEffect);
popEffect.x = bubbles.x;
popEffect.y = bubbles.y;
}
Can anyone see why the popEffect wont position on the current bubble that is being popped? Its acting really weird.
The reason is this:
popEffect.x = bubbles.x;
popEffect.y = bubbles.y;
As far as I can understand, bubbles is a member variable in the class (you are using it in the addBubbles function. Inside onBubbleIsClicked, you provide bubblePop, but do not use it. You are using bubbles instead, which is actually the last instance you've created inside the tick function!
So every time you create popEffect, you actually assign the x and y to the latest created bubblePop.
Some advises:
Do not use member variables that often. They are used WHEN you need to use a variable between functions. In your case, bubbles is a variable that is used only inside the creational function. You even put them into an array! And because you override it with a new one every time you create an instance, your member variables just saves the last one. Is this really needed? Same with popEffect, does anyone else uses it, as it's again just the last one? Such things create mistakes, as you see..
I truly don't understand what this means: if (i < 0). You search if the object you've clicked is not in the array? Well if it is not (how come?!), then what's the meaning of aBubbleArray.splice(i, 1);? Since i < 0, you actually splice with negative value, so you splice some other element! Plan what you want to do, thing logically and then do the actual code. If the object is not in the array, then why do you remove anything from the array?
Start formatting your code better. Read about camel case and variables scope.
Try to manage your logic better. For example this is pretty awkward: BubblePop.parent.removeChild(BubblePop);, as long as you've added it by using stage.addChild(bubbles);. So isn't it more simple to use stage.removeChild(child);? There are some rules in programming (especially in Flash), like 'what added it should remove it'. This will keep you safe in future.
Good luck!

AS3 Array Limits?

I've ran into a weird problem with flash, I have an array of 92 buttons, at first it was all contained in a single array, the buttons up to the first 20th buttons work, the rest don't.
The buttons will take the user to the next scene basically.
So I tried to breakup the array into multiple arrays, so the first array contains the first 20, the second array the 21-40th and so on, the fifth array contains the 81-92 buttons.
The problem now is I will get this error message:
TypeError #1010: A term is undefined and has no properties
and it'll break all the buttons, rendering all the buttons unusable.
Therefore, I commented out the
for (var a=0; a<buttons.length; a++)
{
firstarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
secondarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
thirdarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
fourtharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
//fiftharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
}
in my button spawn function and the buttons from the first to fourth array works flawlessly well except the fifth, which when clicked, nothing happens.
So I tired to create a new function whereby it was only the fiftharray in it and called the new function in the spawner, same error, breaks everything.
Then I thought was there a button naming issue whereby i mistyped something, I took the button names in the fifth array and pasted them into the start of the fourtharray, replacing what was in it plus commenting out the fiftharray from my script.
The once unworkable buttons (81 to 92) worked, but now (61 to 80) didn't.
I tried combining all the arrays using the comarray, but only the first 20 buttons worked.
So I am wondering if is there a fix or something to solve this problem, much help is appreciated!
There is no need to have multiple arrays, remove them. The last array is obviously shorter than the rest and that's messing up your code -> you are pointing to an index that doesn't exist in your last array.
There is actually no need to have an array. You have 92 buttons, that's a nice bunch. Why not to put it to a movieclip instead? What's the need of the array?
Let's assume you select all your buttons and put them inside of movieclip called buttonsClip. Now you can just use this code, without typing all the instance names out to put them to the array (like the tutorial did it... that may work for 8 buttons, but 92... come on :) ):
import flash.display.MovieClip;
import flash.events.MouseEvent;
for(var i:uint=0; i<buttonsClip.numChildren; i++) {
var b:MovieClip = buttonsClip.getChildAt(i) as MovieClip; //Assuming the buttons are movieclips
b.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(e:MouseEvent):void {
trace(e.target.name);
}
I know this Question is currently 8 years old, but maybe this will help another person that is searching for the same thing.
I had pretty much the same problem and in my case it was just that I accidently skipped a number while naming all the Symbols in the libary that I wanted to be in this array. I had 42 Symbols but because of this mistake the function to load the array had 43 and obviously the program was confused about that.

how do you loop thru a listbox and edit each line item in VB express 2010

I have been learning VB for 4 weeks now and I have hit the limits of my knowledge. I sure could use some help from the more experienced programmers!!
Im trying to loop thru my list box and remove the first 4 chars of each line item.
specifically I would like it to behave like this:
first line of list box is selected and sent to string (minus 1st 4 chars)
first line of list box is removed from list box
modified version of first line of list box is added to list box in same position as original
next line of list box is selected...etc....
repeat until entire list box has been modified
Here is a sample of how I'm trying to do this....it almost works :)
Dim test As String
test = ListBox1.SelectedItem.ToString.Substring(4)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
ListBox1.Items.Add(test)
can someone fill in the blanks for me?
Thanks in advance for your help
First, just to double check, you are looping through right?
Next, we can take a look at this line:
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) ListBox1.Items.Add(test)
If I understand this right, you're removing the item at a specific index, then you're adding the new string, test, back into the list. Looking at this, http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.add.aspx, it seems that add will stick this item at the end of the listbox, but you're removing something at a specified index.
Therefore, you're not actually replacing what you probably think you're replacing. That's my best guess. For example, if you remove the 2nd element in your list, but you when you put test back into the list, it's being added onto the end. So something like [a,b,c,d] becomes [a,c,d,b]
Perhaps changing that to:
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) ListBox1.Items.Insert(ListBox1.SelectedIndex, test)
ListBox items can be accessed and edited in this way..
listBox1.BeginUpdate();
try {
for(int i = listBox1.Items.Count - 1; i >= 0 ; i--) {
// do with listBox1.Items[i]
}
} finally {
listBox1.EndUpdate();
}

Array of Colors error. Bug?

K. I'm getting stuck here.
I'm trying to make an array with different color values.
My problem is that when I do...
teamColor[i] = currentColor... all color values in my array turn into the currentColor.
(I would upload more code, but that would be a massive mess, considering that I have code everywhere with references from movie clips that are as far as 3 layers deep. HOWEVER, this would be irrelevant anyways (probably), because I tested this with color values on my main timeline, without any references to or from anything deeply nested)
I'm GUESSING that this is just some horrible bug, but if it's not (and I hope it isn't), please guide me in what to do to fix this problem.
I would like to add that I tried adding strings in there and that the strings remained their original, intended, value, while the color exhibited the same phenomenon.
[Partially resolved]:
I changed my code by creating separate variables for each color instead of putting the variables into an array (not what I really wanted to do, but it works). My code looks like this:
`
if (teamColor != 0)
{
this["team"+teamColor+"Color"] = new ColorTransform(0,0,0,1,currentColor.redOffset,currentColor.greenOffset,currentColor.blueOffset,0)
teamColor = 0
namebox.addboxes()//function in a movieclip
}`
teamColor is now an int that is changed based on which box a user clicks from a movie clip that has a dynamically generated name, based off of what the variable value in a loop was when it was created. (E.G: 'tempboxname[ttns].name = i;')
teamColor is then equal to that name when the user clicks it.
I have another movieclip with colors in it and the above function is called to check if any teamColor change has occurred, and if it has, act accordingly. (The idea of having teamColor equal to 0 is so that if the user clicks twice, nothing changes. I other conditionals for other colors, all within the same function).
That is how I fixed me code.
It's not what I wanted, because it's not an array (meaning a seemingly infinite number of teamColors, and thus, teams) but it'll do for me. If anyone has any suggestions, feel free to suggest.
I'm no ActionScript wiz, but what it looks like to me is that currentColor is an object that is being passed into the array by reference. This means that all array entries that you assigned currentColor will be pointing at the same currentColor object, not a copy. My advice is to make a copy and then assign that into the array.
It would be much better if you could give me more code to look at. For instance, the loop that contains that code segment would be nice. If I find a different error I'll edit my answer.
here i'm creating and then adding simple 0xRRGGBB color objects into a vector. the color objects are then parsed into 0xRRGGBB hexadecimal strings and traced.
certainly it's not exactly what your looking for, but hopefully it will help you.
var red:uint = 0xFF0000;
var green:uint = 0x00FF00;
var blue:uint = 0x0000FF;
var colors:Vector.<uint> = new Vector.<uint>()
colors.push(red, green, blue);
for each (var color:uint in colors)
{
var output:String = color.toString(16);
while (output.length < 6)
output = "0" + output;
trace("0x" + output.toUpperCase());
}
Output:
//0xFF0000
//0x00FF00
//0x0000FF

Resources