我正在尝试解决Weeks的问题,最终我必须在此网站上提问。
问题:-
当用户播放视频广告时,积分会增加,用户会得到奖励。但是,当用户从积分中购买商品时,积分不会减少。
据我了解,这是一个面向对象的问题。
请参阅以下代码段:
Python代码:
class PlayScreen(Screen):
# Function for display sneak peak (Ad Advantage)
def Purchase_For_points(self): #This FUnction will be called when user purchases some hint
// Irrelevant Code
app = MDApp.get_running_app()
app.Num -= 1 #After Purchase Deducted 1 Point
def VideoAdPopup(self):
video_dialog = MDDialog(title = "Watch an Ad",text = "Watch a small video Ad In Exchange of Reward",
size_hint = [1,0.3],auto_dismiss = False,text_button_ok = "Ok",text_button_cancel =
"Cancel",events_callback = self.videocallback)
video_dialog.open()
def videocallback(self,text_of_Selection,popup_widget): #Popup Function for rewarded ads
if(text_of_Selection == "Ok"):
SlipsApp.ads.show_rewarded_ad() #Ad is Shown and rewards will be handled accordingly
else:
print("User did not want to watch ad")
class SlipsApp(MDApp):
# These are our Admob Ad IDs
APP = "ca-app-pub-XXXXXXXXXXXXXXXXXXXX"
BANNER = "ca-app-pub-XXXXXXXXXXXXXXXXXXXX"
INTERSTITIAL = "ca-app-pub-XXXXXXXXXXXXXXX"
REWARDED_VIDEO = "ca-app-pub-XXXXXXXXXXXXXXXXX"
TEST_DEVICE_ID = "3C91BAC5C088815B62389497AC1E309D"
# Creating Ad Instance
ads = KivMob(APP)
#Number oF Rewards(Points)
Num = NumericProperty(5) #Variable for points
def __init__(self, *args,**kwargs):
self.theme_cls.theme_style = "Dark"
super().__init__(*args,**kwargs)
self.reward = Rewards_Handler(self)
#Build Function
def build(self):
# Loading Ads
self.ads.add_test_device(self.TEST_DEVICE_ID)
self.ads.new_banner(self.BANNER, False)
self.ads.new_interstitial(self.INTERSTITIAL)
self.ads.request_banner()
self.ads.set_rewarded_ad_listener(self.reward)
self.ads.load_rewarded_ad(self.REWARDED_VIDEO)
self.ads.show_banner()
return intro()
#Class For Handling Rewards Callback Functions
class Rewards_Handler(RewardedListenerInterface):
def __init__(self,other):
self.game = other
#Overriding Rewards Callback Functions
def on_rewarded(self, reward_name, reward_amount):
self.game.Num += 1 #Reward given 1 Point
print("User Given 1 reward")
Kivy代码:-
<PlayScreen>:
name: 'PlayScreen'
MDLabel:
text:" Points : "+str(app.Num) #Showing Points On Screen
pos_hint:{"top":1.35}
答案 0 :(得分:0)
是的,您是对的,这是一个对象问题。在PlayScreen类的函数Purchase_For_points()中。您将再次创建用于访问App类的对象。这将复制新对象中的所有属性,并每次在运行中的App的新对象中减少Num。
并且,屏幕上显示的是在应用启动时创建的主应用对象的Num变量。 因此,要减少和增加Num变量,您必须有权访问主应用程序对象。
在您的代码中,我看到您已通过 init 方法将App类的访问权限授予Rewards_Handler类。 因此,在Rewards_Handler类中,游戏对象与主应用程序对象相同。
在Rewards_Handler类中创建另一个函数以减少积分,因为这是处理与奖励相关的事情的类
class Rewards_Handler(RewardedListenerInterface):
def decrementReward(self):
self.game.Num -= 1
要从不同的类访问此函数,可以在App类中创建一个函数。它必须是静态方法,这样您才可以创建另一个对象并不会陷入对象中
class SlipsApp(MDApp):
@staticmethod
def decrement(element): #The element will be the main app object.
element.reward.decrementReward()
现在可以访问要使用此函数递减的类。 只需在类中添加 init ()方法
class PlayScreen(Screen):
def __init__(self, **kw):
super().__init__(**kw)
self.app = MDApp.get_running_app() #this is main app object
def Purchase_For_points(self):
SlipsApp.decrement(self.app) #passing main App object.