Application.Quit() won't work - Unity3D unityscript - unity3d

I've little trouble with my simple game.
I'm using this code for menu control:
#pragma strict
var isIZADJI=false;
function OnMouseEnter()
{
renderer.material.color=Color.red;
}
function OnMouseExit()
{
renderer.material.color=Color.white;
}
function OnMouseUp()
{
if(isIZADJI)
{
Application.Quit();
}
else
{
Kontrola_Zivota.ZIVOTI=3;
Application.LoadLevel(1);
}
}
When I click "Play Again" it works fine but when I click "Exit" it just load first level.
Any help here?

I just thought I'd expand on the solution:
I was reading through your code and I see that what you were looking for was a way to identify which button was clicked. I would like to expand on your answer for anyone who want's to know what your solution was. The way to solve this is to create a string variable to check the name of the thing you are clicking (assuming this is GUI Button) and then to change the state of the var isIZADJI based on that eg:
// izlaz[croatian] = exit[english]
// First create a string var for the name of button/GUI/object
var nameOfButton : String;
function OnMouseUp()
{
if(!(nameOfButton == "izlaz"))
{
isIZADJI = false;
}
else
{
isIZDAJI = true;
}
// And then now you can add the rest of your code to quit or load a level
if(isIZADJI)
{
Application.Quit();
}
else
{
Kontrola_Zivota.ZIVOTI=3;
Application.LoadLevel(1);
}
}
I hope this helps anyone else with this problem.

Related

KeyPressed Called Multiple times on Update

I'm trying to add an attack to my character, everything works fine except my button its called multiple times by click (I'm not holding the key down, and its called in average 4 times).
Thats my Update method:
void Update() {
attackArea.enabled = false;
InputCharacter();
MoveAttackArea();
SetAnimation();
ApplyColorFilters();
}
and my InputCharacter method:
void InputCharacter() {
direction = Vector2.zero;
if (Input.GetKey(KeyCode.B)) {
lastAttackTime = currentTime;
Attack();
} else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) {
Move(Vector2.up);
} else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) {
Move(Vector2.down);
} else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) {
Move(Vector2.left);
} else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) {
Move(Vector2.right);
}
}
also my Attack method:
private void Attack() {
Debug.Log("attacking");
animator.SetTrigger("attack");
attackArea.enabled = true;
}
I don't know if the this part its related but:
The log show multiple times and my animations are playing twice (when I play an object destruction animation it runs twice, I don't know if its related)
Replace
Input.GetKey
with
Input.GetKeyDown

Unity3D UI button need multiple click to play or pause audio

Why does my play or pause button need multiple clicks to play or pause the audio? If the target is detected it will display a UI button and if clicked it will play a sound and when clicked again it will pause but I need multiple click to trigger the code.
any solutions? this is my code.
void playSound(string ss)
{
clipTarget = (AudioClip)Resources.Load(ss);
soundTarget.clip = clipTarget;
soundTarget.loop = false;
soundTarget.playOnAwake = false;
soundTarget.ignoreListenerPause = true;
}
if (name == "quezon")
{
if (Translate.GetComponent<Text>().text == "ENGLISH")
{
Narrator.gameObject.SetActive(true);
myGirl.gameObject.SetActive(false);
myBoy.enabled = false;
ButtonAction.GetComponent<Button().onClick.AddListener(delegate {
if (soundTarget.isPlaying)
{
soundTarget.Pause();
btn.image.overrideSprite = Play;
myBoy.enabled = false;
}
else if(!soundTarget.isPlaying)
{
soundTarget.Play();
playSound("sounds/English");
btn.image.overrideSprite = Pause;
myBoy.enabled = true;
}
});
TextDescription.GetComponent<Text>().text = "Manuel L. Quezon was born on August 19, 1878, and died on August 1, 1944. "
+ "He was a Filipino statesman, soldier, and politician who served as president of the Commonwealth of the "
+ "Philippines from 1935 to 1944.";
TextTargetName.GetComponent<Text>().text = name;
}
}
You have to define the button onClick behaviour only once. Like this:
void Awake ()
{
ButtonAction.GetComponent<Button>().onClick.AddListener (() => ToggleSound ());
}
private void ToggleSound ()
{
if (soundTarget.isPlaying)
{
soundTarget.Pause();
btn.image.overrideSprite = Play;
myBoy.enabled = false;
}
else
{
soundTarget.Play();
playSound("sounds/English");
btn.image.overrideSprite = Pause;
myBoy.enabled = true;
}
}
What I think is happening is since you are adding listeners everytime that routine happens it triggers first once, then twice, then thrice and so on.
Thats is the syntax I commonly use for adding behaviour to UI buttons, is easy, you can add parameters and looks clean. I recommend it.
Also if you dont have parameters you can do this and its easier.
ButtonAction.GetComponent<Button().onClick.AddListener (ToggleSound);

checking paragraph property loses the selection

in my vsto addin i have some simple code on a timer
private void MainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!dialogopen & Application.Documents.Count > 0)
{
var doc = Application.ActiveDocument;
Word.InlineShapes shps;
Word.Paragraphs pars;
try
{
pars = doc.Paragraphs;
}
catch (Exception)
{
return;
}
var pars2 = pars.Cast<Word.Paragraph>().ToList();
foreach (var obj in pars2)
{
if (obj.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText )//PROBLEM HERE
{
};
}
}
}
as soon as it reaches the line that checks the outlinelevel, even if i dont do a thing, the selection in the activedocument gets lost
of course the user cant use a plugin which keeps on canceling his selection...
googling didnt give me a thing
thanks
EDIT1
I tried making a static function for checking the styles, but it did not help. Here's the code
static public class Helpers
{
static public Word.Paragraph checkPars(List<Word.Paragraph> pars)
{
return pars.FirstOrDefault();//(x) => x.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText);
}
}
As you can see, I had to remove the actual check, since it was causing the cursor to blink and lose selection
please advise
We use the Application.ScreenUpdating = true; and this keep the selection for all properties in Paragraph except for the Range property.
Then, we tried to access the range via Reflection and this was the solution.
Range rng = (Range)typeof(Paragraph).GetProperty("Range").GetValue(comObj);
We tried to eliminate querying ActiveDocument thinking that that might have had side-effects that were causing the problem but that was not the case.
Then, we confirmed that the selection was not "lost" and screen-updating is the only problem, so we tried restoring the UI with Application.ScreenRefresh and while it did work, it causes the screen to flicker every time the timer fires and this is not good enough.
Finally, knowing that it's only a UI problem, I tried simply switching off Application.ScreenUpdating...
in ThisAddin
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Timer timer = new Timer(2000);
timer.Elapsed += (s, t) =>
{
var scrnUpdating = Application.ScreenUpdating;
Application.ScreenUpdating = false;
MainTimer.onElapsed(Application, t);
if (scrnUpdating)
Application.ScreenUpdating = true;
};
timer.Start();
}
In another class library (note that it's static, I still think this is the best way)...
public static class MainTimer
{
public static void onElapsed (Word.Application state, System.Timers.ElapsedEventArgs e)
{
if (state.Documents.Count > 0)
{
var doc = state.ActiveDocument;
Word.InlineShapes shps;
Word.Paragraphs pars;
try
{
pars = doc.Paragraphs;
}
catch (Exception)
{
return;
}
var pars2 = pars.Cast<Word.Paragraph>()
.Where(p => p.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText)
.Select(p => p) // do stuff with the selected parragraphs...
.ToList();
}
}
}
And this works for me.
The selection remains and is displayed in the UI without flicker.
I also eliminated some premature enumeration from your code: you don't meed the .ToList() before the foreach.

Error #2007: Parameter child must be non-null

Having nothing but errors today! It would be sooo nice to get a bit of help with this one.
I am calling external child swfs for an AIR app; SUPER SIMPLE - calling them with a button, and removing them with a "home" button, that's it. But I get these errors and I cannot publish, even a test, without crashing the entire system. Please help! I have been stuck on this, literally, for weeks!
this is the error:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at pocketDinos_fla::MainTimeline/fl_ClickToGoToAndStopAtFrame_20_1()
[pocketDinos_fla.MainTimeline::frame162:18]
Test Movie terminated.
And this is what I assume is causing the problem on the main timeline (this is ALL the code on the main timeline):
stop();
//home button
button_home.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_01_1,false,0,true);
function fl_ClickToLoadUnloadSWF_01_1(event:MouseEvent):void
{
removeChild(fl_ProLoader_01);
fl_ProLoader_01.unloadAndStop();
fl_ProLoader_01 = null;
}
button_home.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndStopAtFrame_01_1,false,0,true);
function fl_ClickToGoToAndStopAtFrame_01_1(event:MouseEvent):void
{
removeChild(fl_ProLoader_01);
}
button_home.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds_01_1,false,0,true);
function fl_ClickToStopAllSounds_01_1(event:MouseEvent):void
{
SoundMixer.stopAll();
}
button_home.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndStopAtFrame_01_2,false,0,true);
function fl_ClickToGoToAndStopAtFrame_01_2(event:MouseEvent):void
{
gotoAndStop("home");
}
//back to time period button
back_to_triassic.addEventListener(MouseEvent.CLICK,
fl_ClickToLoadUnloadSWF_01_2,false,0,true);
function fl_ClickToLoadUnloadSWF_01_2(event:MouseEvent):void
{
removeChild(fl_ProLoader_01);
fl_ProLoader_01.unloadAndStop();
fl_ProLoader_01 = null;
}
back_to_triassic.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndStopAtFrame_01_3,false,0,true);
function fl_ClickToGoToAndStopAtFrame_01_3(event:MouseEvent):void
{
removeChild(fl_ProLoader_01);
}
back_to_triassic.addEventListener(MouseEvent.CLICK,
fl_ClickToStopAllSounds_01_2,false,0,true);
function fl_ClickToStopAllSounds_01_2(event:MouseEvent):void
{
SoundMixer.stopAll();
}
back_to_triassic.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndStopAtFrame_01_4,false,0,true);
function fl_ClickToGoToAndStopAtFrame_01_4(event:MouseEvent):void
{
gotoAndStop("TRI_home");
}
//start button
start_button_TRI_coelophysis.addEventListener(MouseEvent.CLICK,
fl_ClickToLoadUnloadSWF_01_3,false,0,true);
import fl.display.ProLoader;
import flash.events.Event;
var fl_ProLoader_01:ProLoader;
function fl_ClickToLoadUnloadSWF_01_3(event:MouseEvent):void
{
if(fl_ToLoad_01)
{
fl_ProLoader_01 = new ProLoader();
fl_ProLoader_01.load(new URLRequest("dinofilms/triassic_coelophysis.swf"));
fl_ProLoader_01.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete_01)
addChild(fl_ProLoader_01);
fl_ProLoader_01.x = 0;
fl_ProLoader_01.y = 144;
}
else
{
if(fl_ProLoader_01!=null) {
removeChild(fl_ProLoader_01);
fl_ProLoader_01.unloadAndStop();
fl_ProLoader_01 = null;
}
}
fl_ToLoad_01 = !fl_ToLoad_01;
}
function onComplete_01(e:Event):void {
e.currentTarget.content.addEventListener(Event.ENTER_FRAME,OEF_01);
}
function OEF_01(e:Event):void {
if(e.currentTarget.currentFrame==e.currentTarget.totalFrames) {
e.currentTarget.stop();
e.currentTarget.removeEventListener(Event.ENTER_FRAME,OEF_01);
removeChild(fl_ProLoader_01);
fl_ProLoader_01.unloadAndStop();
fl_ProLoader_01 = null;
}
}
In your function fl_ClickToGoToAndStopAtFrame_01_1 you say removeChild(fl_ProLoader_01); but you also call it in function fl_ClickToLoadUnloadSWF_01_1. Both functions are assigned to the same MovieClip/Button, so it will give an error that it is null, on line 18 where you call removeChild on the same MovieClip/Button again.
I think it got solved!
stop();
//home button
button_home.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndStopAtFrame_01_1,false,0,true);
function fl_ClickToGoToAndStopAtFrame_01_1(event:MouseEvent):void
{
if(fl_ProLoader_01 && fl_ProLoader_01.stage){
fl_ProLoader_01.parent.removeChild(fl_ProLoader_01);}
gotoAndStop("home");
}
button_home.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds_01_1,false,0,true);
function fl_ClickToStopAllSounds_01_1(event:MouseEvent):void
{
SoundMixer.stopAll();
}
//back to time period button
back_to_triassic.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndStopAtFrame_01_3,false,0,true);
function fl_ClickToGoToAndStopAtFrame_01_3(event:MouseEvent):void
{
if(fl_ProLoader_01 && fl_ProLoader_01.stage){
fl_ProLoader_01.parent.removeChild(fl_ProLoader_01);}
gotoAndStop("TRI_home");
}
back_to_triassic.addEventListener(MouseEvent.CLICK,
fl_ClickToStopAllSounds_01_2,false,0,true);
function fl_ClickToStopAllSounds_01_2(event:MouseEvent):void
{
SoundMixer.stopAll();
}
However, I still am having the problem that the child files are not loading on the device itself - strange - they load fine on "test" export, but not when I publish to device (in this case ios). Does anyone have any ideas?

Suppress control-click context menu, gwt 1.6

My gwt 1.6 application intercepts mouse clicks on hyperlinks, so when a user shift-clicks on links to "authors" they get an Edit... dialog box instead of navigating to the author's page. That's working nicely.
I'd now like to allow the user to control-click to select more than one author, but I can't figure out how to suppress the browser's default popup menu. This code handles shift-clicks correctly, but fails in the hosted browser when I control-click and half-fails in Firefox (handleCtrlClick() gets called, but I still get the browser menu):
public void onModuleLoad() {
Event.addNativePreviewHandler(this);
}
//
// Preview events-- look for shift-clicks on paper/author links, and pops up
// edit dialog boxes.
// And looks for control-click to do multiple selection.
//
public void onPreviewNativeEvent(Event.NativePreviewEvent pe) {
NativeEvent e = pe.getNativeEvent();
switch (Event.getTypeInt(e.getType())) {
case Event.ONCLICK:
if (e.getShiftKey()) { handleShiftClick(e); }
if (e.getCtrlKey()) { handleCtrlClick(e); }
break;
case Event.ONCONTEXTMENU:
if (e.getCtrlKey()) { // THIS IS NOT WORKING...
e.preventDefault();
e.stopPropagation();
}
break;
}
}
A breakpoint set inside the ONCONTEXTMENU case is never called.
IIRC ctrl + click is the correct way to select multiple items not ctrl + right click unless you're using a one button mouse (iMac), in that case I can't help you.
Could you provide more details?
Edit:
Why not override the contextmenu (e.g. disable it) then create your own context menu widget (perhaps based on vertical MenuBar + MenuItems) and display it only on Ctrl + RightClick?
In other words you'd create a MouseHandler somewhat like this (pseudo code):
public void onMouseDown(MouseDownEvent event) {
Widget sender = (Widget) event.getSource();
int button = event.getNativeButton();
if (button == NativeEvent.BUTTON_LEFT) {
if(event.is_ctrl_also)
{
// Add to selection
selection = selection + sender;
}
else
{
// Lose selection and start a new one
selection = sender;
}
}
else if(button == NativeEvent.BUTTON_RIGHT) {
if(event.is_ctrl_also)
{
// show context menu
this.contextmenu.show();
}
else
{
// do something else
}
}
return;
}
I've not encountered the bug with Ctrl-Leftclick firing a ContextMenu event, but I'm sure you could also make a workaround for Firefox only using permutations.
I'm getting closer:
public void onModuleLoad() {
Event.addNativePreviewHandler(this); // Catch shift- or control- clicks on links
addContextMenuEventListener(RootPanel.getBodyElement());
}
protected native void addContextMenuEventListener(Element elem) /-{
elem.oncontextmenu = function(e) {
return false; // TODO: only return false if control key down...
};
}-/;
That disables the right-click menu entirely; I'd really like to disable it ONLY if the control key is pressed...