facebook - Handling a Share Button Click on Android Game -
facebook - Handling a Share Button Click on Android Game -
hi want handle click on screen. class not extends activity class cannot utilize trivial method handle click. know how handle click on android app new game development have limited knowledge game. requirement : there share icon on game on screen. want handle icon click when user presses on icon shown bunch of sharing alternative facebook,twitter, skype , many more using createchooser. dont know how that. gameoverscreen code.
public class gameoverscene extends cclayer { //fields private soundmanager _soundmananger; private ccsprite _background; private ccmenu _menu; private ccmenu _restart; private cclabel _yourscore; private cclabel _bestscore; //methods public static ccscene scene() { ccscene scene = ccscene.node(); gameoverscene layer = new gameoverscene(); scene.addchild(layer); homecoming scene; } public gameoverscene() { super(); beetleshotmainactivity.app.sethideadview(false); _soundmananger = soundmanager.sharedsoundresourcemanager(); cgsize size = ccdirector.shareddirector().winsize(); float scalex = size.width / 768.0f; float scaley = size.height / 1024.0f; if (beetleshotmainactivity.app._soundplay == true) { _soundmananger.bstopmenusound(); _soundmananger.bplaymenusound(); } _background = ccsprite.sprite("image/gameover , clear/over_bg.png"); _background.setscalex(scalex); _background.setscaley(scaley); _background.setposition(cgpoint.ccp(size.width/2, size.height/2)); this.addchild(_background); ccmenuitemimage menuitem = ccmenuitemimage.item("image/gameover , clear/maiinnenu_n.png", "image/gameover , clear/maiinnenu_d.png", this, "menuaction"); menuitem.setscalex(scalex); menuitem.setscaley(scaley); _menu = ccmenu.menu(menuitem); _menu.setposition(cgpoint.ccp(size.width*1.0f/2.0f, size.height*3.0f/10.0f)); this.addchild(_menu); menuitem.setposition(cgpoint.ccp(menuitem.getposition().x-50.0f, menuitem.getposition().y)); ccmoveby droptitle = ccmoveby.action(1.5f, cgpoint.ccp(50.0f, 0.0f)); cceasebounceout easedrop = cceasebounceout.action(droptitle); menuitem.runaction(easedrop); ccmenuitemimage restartitem = ccmenuitemimage.item("image/gameover , clear/restart_n.png", "image/gameover , clear/restart_d.png", this, "restartaction"); restartitem.setscalex(scalex); restartitem.setscaley(scaley); _restart = ccmenu.menu(restartitem); _restart.setposition(cgpoint.ccp(size.width/2.0f, (float) (size.height*4.2f/10.0f))); this.addchild(_restart); restartitem.setposition(cgpoint.ccp(restartitem.getposition().x+50.0f, restartitem.getposition().y)); droptitle = ccmoveby.action(1.5f, cgpoint.ccp(-50.0f, 0.0f)); easedrop = cceasebounceout.action(droptitle); restartitem.runaction(easedrop); // ccmenuitemimage sharei = ccmenuitemimage.item("image/mainmenu/share.png", "image/mainmenu/share.png", this, "share"); fb.setscalex(scalex); fb.setscaley(scaley); _restart = ccmenu.menu(sharei); _restart.setposition(cgpoint.ccp(size.width/2.0f, (float) (size.height*1.5f/10.0f))); this.addchild(_restart); sharei.setposition(cgpoint.ccp(restartitem.getposition().x+50.0f, restartitem.getposition().y)); droptitle = ccmoveby.action(1.5f, cgpoint.ccp(-160.0f, 0.0f)); easedrop = cceasebounceout.action(droptitle); sharei.runaction(easedrop); wan handle click.. how do that? want show createchooser dialog when user press on share icon. intent ishare = new intent(intent.action_send); ishare.settype("text/plain"); ishare.putextra(intent.extra_subject, "download game"); ishare.putextra(intent.extra_text, "get game"+" "+"https://play.google.com/store/apps/details?id=com.jdtfans.jdtfootballkickers"); startactivity(intent.createchooser(ishare,"share via")); above code want utilize when user press share icon. how do that? float scalefactor = ccdirector.shareddirector().winsize().height/1024 * 2.3f; _yourscore = cclabel.makelabel(string.format("%d", beetleshotmainactivity.app._score), cgsize.make(120.0f*scalefactor, 60.0f*scalefactor), textalignment.center, "font/imagica.ttf", 24.0f*scalefactor);_yourscore.setposition(cgpoint.ccp(size.width*1.32f/2.0f, (float) (size.height*1.12/2.0f))); this.addchild(_yourscore); _yourscore.setcolor(cccolor3b.ccblue); // add together _bestscore = cclabel.makelabel(string.format("%d", gamescene._hscorevalue), cgsize.make(120.0f*scalefactor, 60.0f*scalefactor), textalignment.center, "font/imagica.ttf", 24.0f*scalefactor);_bestscore.setposition(cgpoint.ccp(size.width*1.32f/2.0f, (float) (size.height*1/2.0f))); this.addchild(_bestscore); _bestscore.setcolor(cccolor3b.ccblue); // switch(beetleshotmainactivity.app._locationlevel) { case 1: beetleshotmainactivity.app._level = 1; break; case 2: beetleshotmainactivity.app._level = 2; break; case 3: beetleshotmainactivity.app._level = 3; break; default: break; } int currentscore = beetleshotmainactivity.app._score; beetleshotmainactivity.app._score = 0; if (beetleshotmainactivity.app._location == 1) { switch(beetleshotmainactivity.app._locationlevel) { case 1: beetleshotmainactivity.app.loadscore("score1"); break; case 2: beetleshotmainactivity.app.loadscore("score2"); break; case 3: beetleshotmainactivity.app.loadscore("score3"); break; default: break; } } if (currentscore > beetleshotmainactivity.app._score) { beetleshotmainactivity.app._score = currentscore; if (beetleshotmainactivity.app._location == 1) { switch(beetleshotmainactivity.app._locationlevel) { case 1: beetleshotmainactivity.app.savescore("score1"); break; case 2: beetleshotmainactivity.app.savescore("score2"); break; case 3: beetleshotmainactivity.app.savescore("score3"); break; default: break; } } } } public void menuaction(object sender) { if (beetleshotmainactivity.app._soundplay == true) _soundmananger.eplaybuttonclicksound(); ccdirector.shareddirector().replacescene(ccfadetransition.transition(0.7f, menuscene.scene())); } public void restartaction(object sender) { if (beetleshotmainactivity.app._soundplay == true) _soundmananger.eplaybuttonclicksound(); ccdirector.shareddirector().replacescene(ccfadetransition.transition(0.7f, gamescene.scene())); } } activity class have in project. /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); app = this; mcontext = this; requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setrequestedorientation(activityinfo.screen_orientation_portrait); ccmacros.cclog("beetleshot game", "oncreate"); relativelayout layout = new relativelayout(this); layout.setlayoutparams(new relativelayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent)); mglsurfaceview = new ccglsurfaceview(this); // create adview adview = new adview(this, adsize.banner, admob_publish_id); // initiate generic request load advertisement adview.loadad(new adrequest()); adview.setadlistener(this); // add together adview relativelayout.layoutparams params = new relativelayout.layoutparams( layoutparams.wrap_content, layoutparams.wrap_content); params.addrule(relativelayout.align_parent_bottom, relativelayout.true); params.addrule(relativelayout.center_horizontal, relativelayout.true); adview.setlayoutparams(params); layout.addview(mglsurfaceview); layout.addview(adview); //// // create interstitial interstitial = new interstitialad(this, "**********"); // load interstitialad ads.loadinterstitialad(interstitial); // set advertisement listener utilize callbacks below interstitial.setadlistener(this); setcontentview(layout); ccdirector.shareddirector().attachinview(mglsurfaceview); ccdirector.shareddirector().setdisplayfps(false); ccdirector.shareddirector().setanimationinterval(1.0f / 30); cctexture2d.setdefaultalphapixelformat(config.argb_8888); _soundplay = true; _playersound = true; _controlshow = false; _playerpos = 1; _level = 1; _firstbonus = 0; _secondbonus = 0; _thirdbonus = 0; _locationlevel = 1; ccdirector.shareddirector().runwithscene(logoscene.scene()); } @override protected void onstart() { super.onstart(); } @override protected void onresume() { super.onresume(); ccdirector.shareddirector().onresume(); } @override protected void onpause() { super.onpause(); soundmanager.sharedsoundresourcemanager()._soundengine.pausesound(); ccdirector.shareddirector().onpause(); } @override protected void ondestroy() { super.ondestroy(); ccdirector.shareddirector().end(); soundmanager.releasesoundresourcemanager(); } public void loadscore (string filename) { seek { ccmacros.cclog("file reading", "reading scores " + filename); fileinputstream fin = openfileinput(filename); inputstreamreader isr = new inputstreamreader(fin); char[] buf = new char[6]; isr.read(buf); _score = integer.parseint(new string(buf).trim()); isr.close(); fin.close(); ccmacros.cclog("file reading result", "" + _score); } grab (exception e) { } } public void savescore (string filename) { seek { ccmacros.cclog("file writing", "writing scores " + filename); fileoutputstream fout = openfileoutput(filename, mode_world_readable); outputstreamwriter osw = new outputstreamwriter(fout); osw.write("" + _score); osw.flush(); osw.close(); fout.close(); } grab (exception e) { } } public cgpoint getbufplayerpos() { homecoming _m_playerpos; } public void setbufplayerpos (cgpoint rpos) { _m_playerpos = rpos; } // admob setting public void ondismissscreen(ad arg0) { // todo auto-generated method stub } public void onfailedtoreceivead(ad arg0, errorcode arg1) { // todo auto-generated method stub } public void onleaveapplication(ad arg0) { // todo auto-generated method stub } public void onpresentscreen(ad arg0) { // todo auto-generated method stub } public void onreceivead(ad arg0) { // todo auto-generated method stub //log.d(tag, "+++++++++++++ new admob has received! ++++++++++++++"); } handler handler = new handler() { public void handlemessage(message msg) { if(msg.what == 0) { adview.setvisibility(view.invisible); } else { adview.setvisibility(view.visible); } } }; public void sethideadview(boolean bhide) { if(bhide) { handler.sendemptymessage(0); } else { handler.sendemptymessage(1); } } }
in main activity class, define share method:
public void share(){ runonuithread(new runnable(){ @override public void run() { intent ishare = new intent(intent.action_send); ishare.settype("text/plain"); ishare.putextra(intent.extra_subject, "download game"); ishare.putextra(intent.extra_text, "get game"+" "+"https://play.google.com/store/apps/details?id=com.jdtfans.jdtfootballkickers"); try{ startactivity(intent.createchooser(ishare,"share via")); }catch (android.content.activitynotfoundexception ex) { toast.maketext(getapplicationcontext(), "no sharing client installed, toast.length_short) .show(); } } }); }
then in gameoverscene class, uncomment line below , alter "share" "shareaction":
ccmenuitemimage sharei = ccmenuitemimage.item("image/mainmenu/share.png", "image/mainmenu/share.png", this, "shareaction");
then, define shareaction method below restartaction() method:
public void shareaction(object sender) { if (beetleshotmainactivity.app._soundplay == true) _soundmananger.eplaybuttonclicksound(); beetleshotmainactivity.app.share(); }
android facebook twitter onclicklistener cocos2d-android
Comments
Post a Comment